I''m trying to figure out why both ''self.name'' and ''name'' work in a model class. For example: class Category < ActiveRecord::Base before_save :set_permalink private def set_permalink # both work self.permalink = name self.permalink = self.name # doesn''t work permalink = name @permalink = name @permalink = @name self.permalink = @name end end I thought that attributes in a class were set via instance methods, but ''self.permalink = @name'' doesn''t work, but ''name'' (which looks like a local variable to me) works fine. And self.permalink works, but @permalink doesn''t. I''m still new to Ruby and classes so it''s probably something simple I don''t understand, so any help would be appreciated. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Cale
2007-Jul-01 16:23 UTC
Re: ActiveRecord: why does ''self.name'' and ''name'' both work?
Basically name and self.name are interpreted by ruby as methods.. because your model when mapped to its corresponding table has a column called ''name'', rails creates the relevant method instance.name. Therefore your reference to name and self.name are calling the same method.. as name (without the self.) is scoped to the instance method (as its inside an instance method itself), and self evaluates to the instance, therefore it calls the instance method ''name''.. # both work self.permalink = name <-- calls method name, and its value is passed to the method ''permalink'' (again, not an variable really) self.permalink = self.name <-- calls same method.. as self. evaluates to the instance # doesn''t work permalink = name <-- ruby is probably getting confused here.. which is probably why its not working.. @permalink = name <-- again this is probably being set .. but it wont update your model as it is saved.. because @permalink is a var self.permalink is the method you need to call to update your model.. @permalink = @name <--- again they are not instance vars. .. so @permalink => nil but you wont see it in your db.. self.permalink = @name <-- @name doesnt exist .. so will be nil.. I hope that makes sense.. sunday afternoon in the office has left my brain a bit fried.. (mainly because im writing actionscript..) Good luck! Jason. On 1 Jul 2007, at 17:10, rob wrote: I''m trying to figure out why both ''self.name'' and ''name'' work in a model class. For example: class Category < ActiveRecord::Base before_save :set_permalink private def set_permalink # both work self.permalink = name self.permalink = self.name # doesn''t work permalink = name @permalink = name @permalink = @name self.permalink = @name end end I thought that attributes in a class were set via instance methods, but ''self.permalink = @name'' doesn''t work, but ''name'' (which looks like a local variable to me) works fine. And self.permalink works, but @permalink doesn''t. I''m still new to Ruby and classes so it''s probably something simple I don''t understand, so any help would be appreciated. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---