# Piece of code that runs only once

**URL:** <https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636>\
**Category:** Rails\
**Created:** [September 16, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636 "2009-09-16T21:57:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 16, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/1 "2009-09-16T21:57:09Z")

</div>

hi, an strange error is happening here

in User model i have the following method:  
def falta\_votar  
Category.find(:all) - votes.collect {|v| v.category}  
end

and in the default.html.erb layout i have:  
\<%= current\_user.falta\_votar.size %\>

but it only works once, when i try to access another page i get an  
error message saying that the falta\_votar method is nil, then i have  
to restart the server to access the site

why is this happening?

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 16, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/2 "2009-09-16T22:12:23Z")

</div>

also, i’m running the latest rails version, 2.3.4

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 16, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/3 "2009-09-16T23:41:56Z")

</div>

2009/9/16 PaÅ­lo Geyer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> but it only works once, when i try to access another page i get an  
> error message saying that the falta\_votar method is nil, then i have  
> to restart the server to access the site
> 
> why is this happening?

The current\_user is likely what is nil, not the User method. Just  
guessing, I’d make sure the current\_user is actually being  
instantiated on subsequent requests.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 17, 2009, 6:50am UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/4 "2009-09-17T06:50:59Z")

</div>

no, current\_user isn’t nil

Extracted source (around line #21):

18:   
  
19: VocÃª jÃ¡ votou em \<%= current\_user.votes.count %\> categorias\<br /

> 

20:  
21: \<%= current\_user.falta\_votar.size %\>

the current.user.votes works fine, but current\_user.falta\_votar  
doesn’t work when i try to reload the page, i don’t know why it would  
become nil, but is happening

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 17, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/5 "2009-09-17T12:39:53Z")

</div>

Hi Paulo,

On Wed, 2009-09-16 at 21:48 -0700, Paulo Geyer wrote:

Sorry for coming in late. I haven’t followed the conversation so  
perhaps this will be off-track but maybe not.

> the current.user.votes works fine,  
> Not having seen the controller code, I’m guessing here…

My guess is that ‘current\_user’ is a method (not a variable/object),  
that returns a User object and that the method is invoked via a  
before\_filter.

> but current\_user.falta\_votar  
> doesn’t work when i try to reload the page, i don’t know why it would  
> become nil, but is happening

Reloading the page may not be causing the before\_filter to run. From  
where are you calling it? Check to make sure that it’s called from the  
controller getting instantiated via the method call invoked on the page  
reload.

Another explanation might be that ‘current\_user.votes.count’ is working  
because ‘votes’, as in current\_user.votes’, is an association, whereas  
‘falta\_votar’ is a method on a model. It’s too early here for me to  
figure out / put into words why that might matter 😉

In either case, you’ll be safer moving both of those into instance  
variables in your controller(s) and then using the instance variables in  
the views.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![PaSSS\_lo\_Geyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb9ed0/32.png) [@PaSSS\_lo\_Geyer](https://www.ruby-forum.com/u/PaSSS_lo_Geyer)\
**Post date:** [September 17, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/piece-of-code-that-runs-only-once/175636/6 "2009-09-17T19:45:29Z")

</div>

using the “falta\_votar” method doesn’t cause any problem, but the  
“votes.collect {|v| v.category}” inside the “falta\_votar” method seems  
to cause the problem, it runs only once
