Help me understand association attributes


#1

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


#2

Thank you.
-Kelly


#3

Kelly Dwight F. wrote:

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?

Have a read of this:
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/b0b54ca108fb589d


We develop, watch us RoR, in numbers too big to ignore.