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

Hello Brett,

end
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.

Jean-François,

 self.campaign_start = Time.now

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_______________________________________________
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails