Kelly Dwight Felkins
2006-Jan-03 01:55 UTC
[Rails] help me understand association attributes
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/fbfd3e2f/attachment.html
Mark Reginald James
2006-Jan-03 03:33 UTC
[Rails] Re: help me understand association attributes
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/browse_frm/thread/b0b54ca108fb589d -- We develop, watch us RoR, in numbers too big to ignore.
Kelly Dwight Felkins
2006-Jan-03 06:48 UTC
[Rails] Re: help me understand association attributes
Thank you. -Kelly On 1/2/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> > 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/browse_frm/thread/b0b54ca108fb589d > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/a2cfd162/attachment.html
Maybe Matching Threads
- models and validation problem
- why doesn't this work (setting attribute in "before_create")
- wrong id for activerecord object when using :joins in mysql
- inexpensive ways to make a rails application highly available? mysql replication?
- scgi apache routes help needed