# Before\_save only works at console

**URL:** <https://www.ruby-forum.com/t/before-save-only-works-at-console/152315>\
**Category:** Rails\
**Created:** [November 1, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/before-save-only-works-at-console/152315 "2008-11-01T23:48:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [November 1, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/before-save-only-works-at-console/152315/1 "2008-11-01T23:48:48Z")

</div>

Hi all. I’ve the following comments’ model:

class Comment \< ActiveRecord::Base  
validates\_presence\_of :commentary, :anon

belongs\_to :commentary, :polymorphic =\> true

attr\_accessor :anon #anon is an virtual attribute.

before\_save :anonymize

protected  
def anonymize  
if self.anon == 1  
self.author = “Anonymous”  
self.email = “removed\_email\_address@domain.invalid”  
end  
end  
end

If I bring up my console:

> > c = Comment.new  
> > =\> #\<Comment id: nil, commentary\_id: nil, commentary\_type: nil, author:  
> > nil,  
> > email: nil, comment: nil, created\_at: nil, updated\_at: nil\>  
> > c.author = “foo”  
> > =\> “foo”  
> > c.email = “bar”  
> > =\> “bar”  
> > c.commentary\_type = “Article”; c.commentary\_id = 6  
> > =\> 6  
> > c.anon = 1  
> > =\> 1  
> > c.save!  
> > =\> true  
> > c  
> > =\> #\<Comment id: 26, commentary\_id: 6, commentary\_type: “Article”,  
> > author:  
> > “Anonymous”, email: “removed\_email\_address@domain.invalid”, comment: nil, created\_at:  
> > “2008-11-01 22:18:06”, updated\_at: “2008-11-01 22:18:06”\>

All works fine. But it doesn’t work from my view:

[http://pastie.org/305633](http://pastie.org/305633)

From my view, it saves using comment\_author.value and  
comment\_email.value.  
What am I missing?

## Best regards,

## Davi V.

E-mail: davividal arroba siscompar ponto com ponto br  
MSN : davividal arroba msn ponto com  
GTalk : davividal arroba gmail ponto com  
Skype : davividal  
YIM : davi\_vidal  
ICQ : 138815296

---

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [November 2, 2008, 12:12am UTC](https://www.ruby-forum.com/t/before-save-only-works-at-console/152315/2 "2008-11-02T00:12:50Z")

</div>

On 1 Nov 2008, at 22:46, Davi V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > c.commentary\_type = “Article”; c.commentary\_id = 6
> 
> All works fine. But it doesn’t work from my view:
> 
> [http://pastie.org/305633](http://pastie.org/305633)
> 
> From my view, it saves using comment\_author.value and  
> comment\_email.value.  
> What am I missing?

anon will be the string “1” and not the integer 1

Fred

---

<div class="post-metadata">

**Author:** ![Davi\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Davi\_V](https://www.ruby-forum.com/u/Davi_V)\
**Post date:** [November 2, 2008, 12:18am UTC](https://www.ruby-forum.com/t/before-save-only-works-at-console/152315/3 "2008-11-02T00:18:41Z")

</div>

On Saturday 01 November 2008 21:11:59 Frederick C. wrote:

> On 1 Nov 2008, at 22:46, Davi V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> […]
> 
> > From my view, it saves using comment\_author.value and  
> > comment\_email.value.  
> > What am I missing?
> 
> anon will be the string “1” and not the integer 1

Ops! Lack of coffee. 🙂  
Thank you very much, Fred.

Changed from “self.anon” to “self.anon.to\_i”.

## – Davi V.

E-mail: davividal arroba siscompar ponto com ponto br  
MSN : davividal arroba msn ponto com  
GTalk : davividal arroba gmail ponto com  
Skype : davividal  
YIM : davi\_vidal  
ICQ : 138815296
