Brett Walker
2006-Apr-21 20:10 UTC
[Rails] Using before_create and conflicting setter method...
Hi, I am having trouble using before_create when I have an specialized setter method: before_save :set_campaign_start #----------------------------------------------------------------------- ------- def set_campaign_start self.campaign_start = Time.now end # We want the date to always start at midnight #----------------------------------------------------------------------- ------- def campaign_start=(new_time) @campaign_start = new_time.utc.at_beginning_of_day end It sets @campaign_start in the object, but not the actual attribute, and therefore not in the DB. If I remove the setter method, the before_create works great. What is the correct way to do this? Thanks, Brett
Jean-François
2006-Apr-21 20:46 UTC
[Rails] Using before_create and conflicting setter method...
Hello Brett,> I am having trouble using before_create when I have an specialized > setter method: > > before_save :set_campaign_start > > > #----------------------------------------------------------------------- > ------- > def set_campaign_start > self.campaign_start = Time.now > end > > # We want the date to always start at midnight > > #----------------------------------------------------------------------- > ------- > def campaign_start=(new_time) > @campaign_start = new_time.utc.at_beginning_of_day > end > > It sets @campaign_start in the object, but not the actual attribute, > and therefore not in the DB. If I remove the setter method, the > before_create works great. What is the correct way to do this?def campaign_start=(new_time) write_attribute("campaign_start", new_time.utc.at_beginning_of_day) end But I''m not sure I''ve understood your pb correctly. -- Jean-Fran?ois. -- ? la renverse.
Brett Walker
2006-Apr-22 15:07 UTC
[Rails] Using before_create and conflicting setter method...
Jean-Fran?ois,>> I am having trouble using before_create when I have an specialized >> setter method: >> >> before_save :set_campaign_start >> >> >> #-------------------------------------------------------------------- >> --- >> ------- >> def set_campaign_start >> self.campaign_start = Time.now >> end >> >> # We want the date to always start at midnight >> >> #-------------------------------------------------------------------- >> --- >> ------- >> def campaign_start=(new_time) >> @campaign_start = new_time.utc.at_beginning_of_day >> end >> >> It sets @campaign_start in the object, but not the actual attribute, >> and therefore not in the DB. If I remove the setter method, the >> before_create works great. What is the correct way to do this? > > def campaign_start=(new_time) > write_attribute("campaign_start", new_time.utc.at_beginning_of_day) > end > > But I''m not sure I''ve understood your pb correctly.That did the trick. Most examples I had seen showed using the instance variable when overriding the assignment method. But I guess since I''m trying to do an assignment in the before_create, some connection was not made yet. The "campaign_start" attribute was not getting set, just the instance variable. Anyway, worked like a charm. Thanks! Brett