# Before\_save gotcha

**URL:** <https://www.ruby-forum.com/t/before-save-gotcha/53592>\
**Category:** Rails\
**Created:** [February 10, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592 "2006-02-10T21:46:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [February 10, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592/1 "2006-02-10T21:46:33Z")

</div>

Is it will known and accepted that before\_save triggers should return  
true? I didn’t notice this before but now I see it in the  
documentation.

Here is what I’m doing in my model:

def before\_save  
if self.has\_album?  
self.visible = self.album.visible?  
end  
end

That results in the expected result when album.visible? is true - but  
not when it is false. To make it work correctly, I have to explicitly  
return true at the end.

def before\_save  
if self.has\_album?  
self.visible = self.album.visible?  
end  
true  
end

Is that what I am supposed to do? It feels so wrong and C-like… is  
there a better way?

-Pawel

---

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [February 10, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592/2 "2006-02-10T21:50:24Z")

</div>

On Fri, Feb 10, 2006 at 12:46:18PM -0800, Pawel S. wrote:

> Is it will known and accepted that before\_save triggers should return  
> true? I didn’t notice this before but now I see it in the  
> documentation.

It is not that they should return true, it is that they should _not_  
return  
false.

Returning nil or any boolean true value is fine, but the request chain  
will  
be halted if a before\_filter returns false.

marcel

---

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [February 10, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592/3 "2006-02-10T22:02:35Z")

</div>

On 2/10/06, Marcel Molina Jr. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, Feb 10, 2006 at 12:46:18PM -0800, Pawel S. wrote:
> 
> > Is it will known and accepted that before\_save triggers should return  
> > true? I didn’t notice this before but now I see it in the  
> > documentation.
> 
> It is not that they should return true, it is that they should _not_ return  
> false.

Yes - sorry, I worded that unclearly. In my head, anything not false is  
true. 🙂

Still, it’s easy to accidentally return false when updating boolean  
fields like that - it took me a while to track down too. I’m afraid I  
may start putting a true at the end of all of my trigger callbacks out  
of sheer paranoia!

-Pawel

---

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [February 10, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592/4 "2006-02-10T22:06:11Z")

</div>

> not false is true.  
> true

I had too…its Friday 🙂

---

<div class="post-metadata">

**Author:** ![makenai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8797f3/32.png) [@makenai](https://www.ruby-forum.com/u/makenai)\
**Post date:** [February 10, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/before-save-gotcha/53592/5 "2006-02-10T22:12:49Z")

</div>

On Fri, Feb 10, 2006 at 01:01:18PM -0800, Pawel S. wrote:

> Still, it’s easy to accidentally return false when updating boolean  
> fields like that - it took me a while to track down too. I’m afraid I  
> may start putting a true at the end of all of my trigger callbacks out  
> of sheer paranoia!

You should always be able to know if an expression is going to return  
nil or  
false in the case that it is boolean false. Usually it will return nil  
unless  
you explicitly implement something to return false or are using  
operators  
like ! or && that result in true or false. So, no need to be paranoid.  
Just  
be mindful. Your functional tests will catch you if you slip up and the  
development log will indicate if a before\_filter halted the filter  
chain.

marcel
