# Using before\_create and conflicting setter method

**URL:** <https://www.ruby-forum.com/t/using-before-create-and-conflicting-setter-method/58463>\
**Category:** Rails\
**Created:** [April 21, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/using-before-create-and-conflicting-setter-method/58463 "2006-04-21T22:12:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [April 21, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/using-before-create-and-conflicting-setter-method/58463/1 "2006-04-21T22:12:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [April 21, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/using-before-create-and-conflicting-setter-method/58463/2 "2006-04-21T22:46:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vor\_lord](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/3ec8ea/32.png) [@vor\_lord](https://www.ruby-forum.com/u/vor_lord)\
**Post date:** [April 22, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/using-before-create-and-conflicting-setter-method/58463/3 "2006-04-22T17:10:19Z")

</div>

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  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
