Forum: Ruby on Rails attribute accessor (counter_cache)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Andrew (Guest)
on 2007-03-20 23:22

I am rather confused about what I'm seeing with an attribute of mine
that has been defined as a counter_cache.

'votes_count' is the attribute that is a counter_cache var.

If I do this in my view...

<% p test.attributes %>
<% p test[:votes_count] %>
<% p test.votes_count %>

I get this printed out...

{"votes_count"=>11, "id"=>2}

Why does test.votes_count give me 1?

If I do a test.reload before calling those prints, then test.votes_count
gives me the correct value of 11.

Can somebody tell me why I'm seeing these results?

This topic is locked and can not be replied to.