Forum: Ruby on Rails help me understand association attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2006-01-03 02:56
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-03 04:36
(Received via mailing list)
Kelly Dwight Felkins 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/brow...

--
We develop, watch us RoR, in numbers too big to ignore.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2006-01-03 07:49
(Received via mailing list)
Thank you.
-Kelly
This topic is locked and can not be replied to.