Piece of code that runs only once

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?

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

2009/9/16 PaÅ­lo Geyer [email protected]:

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/

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

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 :wink:

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

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