Max Williams
2008-Sep-12 10:33 UTC
Get an attribute''s value without calling the AR get method
I want to override a get method to try to get a value from an associated object first, and then get the object''s own value if the associated object returned nil. But, this requires me to avoid calling a get method from inside the same get method. To be specific: there are two classes, School and RegisteredInfo. Normally, the RegisteredInfo class holds the official data for a school - address, postcode, phone number etc, and in most cases a school has_one registered info. So, if i want to get the address for a school, i normally pull it out of the associated RegisteredInfo object: address = @school.registered_info.address However, a school might not always have a registered info, and so has its own address field for this purpose. So, i want to make the address method as follows: #in school.rb def address if self.registered_info return self.registered_info.address else return self.address end end However, this sets up a circular loop, where the method will keep calling itself. I''m sure that i''ve used a private/protected method called something like "read_attribute" that pulled the value out of the database without calling the get method again. However, i can''t find it in the api (i''m using rails 2.0.2). Can anyone help, or provide a nicer alternative to this problem? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-12 10:40 UTC
Re: Get an attribute''s value without calling the AR get method
On 12 Sep 2008, at 11:33, Max Williams wrote:> > > However, this sets up a circular loop, where the method will keep > calling itself. I''m sure that i''ve used a private/protected method > called something like "read_attribute" that pulled the value out of > the > database without calling the get method again. However, i can''t > find it > in the api (i''m using rails 2.0.2).read_attribute is still there even if it''s not documented. Or you can use self[:address] Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2008-Sep-12 10:46 UTC
Re: Get an attribute''s value without calling the AR get method
Frederick Cheung wrote:> > read_attribute is still there even if it''s not documented. Or you can > use self[:address] > > FredFantastic - thanks Fred! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---