Leon Bogaert
2008-Sep-22 22:05 UTC
Activerecord: use attributes methods instead of attrs var
This is the case: when you call save() on an activerecord::base object it goes through all the attrs[:] and saves them. But I have a lot of attributes overriden. For example: class Tester def name write_attribute(:name, ''This is just a silly example!'') read_attribute(:name) end end t = Tester.new t.save But when I do save() it doesn''t get saved to the database if I haven''t called the name() method. When I add a validation, the method somewhere gets called and it is added to the attrs instance variable. Is there a way, without adding validations for every attribute, to call the attribute methods before save? I thought about using a before_save filter, but maybe someone would new a better/other way. I''m just curious if it''s possible. Maybe I''m on the wrong track and there may be better ways to override the attribute getters? Thanks in advance! -- 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 -~----------~----~----~----~------~----~------~--~---
Brandon Keepers
2008-Sep-23 00:10 UTC
Re: Activerecord: use attributes methods instead of attrs var
On Mon, Sep 22, 2008 at 6:05 PM, Leon Bogaert < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > This is the case: when you call save() on an activerecord::base object > it goes through all the attrs[:] and saves them. But I have a lot of > attributes overriden. > For example: > > class Tester > def name > write_attribute(:name, ''This is just a silly example!'') > read_attribute(:name) > end > end > > t = Tester.new > t.save > > But when I do save() it doesn''t get saved to the database if I haven''t > called the name() method. > > When I add a validation, the method somewhere gets called and it is > added to the attrs instance variable. > > Is there a way, without adding validations for every attribute, to call > the attribute methods before save? > > I thought about using a before_save filter, but maybe someone would new > a better/other way. > > I''m just curious if it''s possible. Maybe I''m on the wrong track and > there may be better ways to override the attribute getters? >You''re essentially trying to set default values for your attributes, right? If so, I would use the #after_initialize callback, which AR calls when a new instance of the class is created. class Tester < ActiveRecord::Base def after_initialize self.name ||= ''Default name'' end end Brandon -- -------------------------------------------------------------------------------- Sessions by Collective Idea: Ruby on Rails training in a vacation setting http://sessions.collectiveidea.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 -~----------~----~----~----~------~----~------~--~---
zuo peng
2008-Sep-23 02:14 UTC
Re: Activerecord: use attributes methods instead of attrs var
I would recommend the before_create callback. it seems you do not need to overwrite the the attributes everytime you update it, and before_create work only for new records. after_initialize has performance issue and overwrite the default getter may cause side effects. hope that helps. Regards, Peng Zuo On Tue, Sep 23, 2008 at 8:10 AM, Brandon Keepers <brandon-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote:> On Mon, Sep 22, 2008 at 6:05 PM, Leon Bogaert > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> This is the case: when you call save() on an activerecord::base object >> it goes through all the attrs[:] and saves them. But I have a lot of >> attributes overriden. >> For example: >> >> class Tester >> def name >> write_attribute(:name, ''This is just a silly example!'') >> read_attribute(:name) >> end >> end >> >> t = Tester.new >> t.save >> >> But when I do save() it doesn''t get saved to the database if I haven''t >> called the name() method. >> >> When I add a validation, the method somewhere gets called and it is >> added to the attrs instance variable. >> >> Is there a way, without adding validations for every attribute, to call >> the attribute methods before save? >> >> I thought about using a before_save filter, but maybe someone would new >> a better/other way. >> >> I''m just curious if it''s possible. Maybe I''m on the wrong track and >> there may be better ways to override the attribute getters? > > You''re essentially trying to set default values for your attributes, right? > If so, I would use the #after_initialize callback, which AR calls when a new > instance of the class is created. > > class Tester < ActiveRecord::Base > def after_initialize > self.name ||= ''Default name'' > end > end > > Brandon > -- > -------------------------------------------------------------------------------- > Sessions by Collective Idea: Ruby on Rails training in a vacation setting > http://sessions.collectiveidea.com > > > >-- Murray Walker - "And now, excuse me while I interrupt myself." --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---