I have a model with a belongs_to association. The first model is called
Thing and the associated model is User. I’m attempting to assign the
user to
the thing.
To accomplish this I used a before_save method, like this:
class Thing < ActiveRecord::Base
belongs_to :created_by, :class_name => “User”, :foreign_key =>
“created_by_user_id”
def before_create
self.created_by = get_user()
end
I had trouble getting the assignment to work. I initially tried simply
created_by = get_user()
(no ‘self’) I don’t know why ‘self’ made a difference in this case. Can
anyone explain this to me?
Thank you.
-Kelly