Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:46:08+00:00 2026-05-24T03:46:08+00:00

I have a Topic model: class Topic < ActiveRecord::Base belongs_to :user def validate_on_update errors.add(:user,

  • 0

I have a Topic model:

class Topic < ActiveRecord::Base
  belongs_to :user

  def validate_on_update
    errors.add(:user, "Only the topic creator can update the topc") if
      self.user != user;
  end
end

I would like to check before every update that the existing topic.user is the same with the user that is trying to update the model.

I think that

self.user != user 

is not working but I do not know how to fix that!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:46:09+00:00Added an answer on May 24, 2026 at 3:46 am

    You need to find the record in the controller before doing that, so you can do this in your controller action:

    @topic = current_user.topics.find(params[:id])
    

    This will trigger an exception that you can easily catch or leave it.

    This is the best method to ensure data integrity, unless you’re tinkering in other places of the app and you need to create Topics not in controllers.

    If you have such need, it’s not bad to have a validation rule in the model to ensure major data integrity, but the model does need to know the user, that’s only accessible from the controller.

    My recommendation is that you assign the user controller-side or just use scopes like:

    current_user.topics.create(params[:topic])
    

    This way you are sure that the user is the same in question, and this invalidates the need to do another validation if it’s the only place you’re calling topic creation.

    If you are unsure and wants to game on with a validate_on_update I suggest creating a virtual attribute like so:

    attr_accessor :this_user
    

    But in any case you’d pass this via controller, since your model should know nothing about the current logged in user:

    @topic = Topic.new(params[:topic])
    @topic.this_user = current_user # or @topic.user_id and check for a attr_changed?
    

    Update: adding example as requested

    # @topic = Topic.new(params[:topic])
    # @topic.this_user = current_user
    
    class Topic < ActiveRecord::Base
      belongs_to :user
      attr_accessor :this_user
    
      def validate_on_update
        # Make sure that this_user is an instance of User, otherwise just use the id
        errors.add(:user, "Only the topic creator can update the topic") if user_id != this_user.id;
      end
    end
    

    Update:

    another suggestion is to use:

    attr_readonly :user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model: class Topic < ActiveRecord::Base define_index do indexes title, :sortable =>
I have model: class Topic < ActiveRecord::Base attr_accessible :title, :description has_many :comments, :dependent =>
I have the following Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
I have declared models in AppEngine's models.py: class Post(db.Model): topic = db.ReferenceProperty(Topic, collection_name='posts', verbose_name=_('Topic'))
In my model I have: class Poll(models.Model): topic = models.CharField(max_length=200) tags = models.ManyToManyField(Tag) I'm
I have a view model (below). public class TopicsViewModel { public Topic Topic {
I have many Topic objects and each Topic hasMany posts:Post How can I order
I have encountered this topic lately and couldn't understand why they are needed. Can
For example I have 2 models that Topic and Comment . 1 Topic can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.