counter_cache bug not resolved?

3 views
Skip to first unread message

jeanmartin

unread,
Jan 15, 2008, 6:42:03 AM1/15/08
to Ruby on Rails: Core
Hi,
I've already filed a bug: http://dev.rubyonrails.org/ticket/10804
I'm still not sure if I'm missing something:

When I update a child by setting the association method to nil, the
counter of the parent does not get decremented. For a code example see
the first answer to my ticket.

The problem seems to be that the @owner variable in
activerecord/lib/active_record/associations/belongs_to_association.rb
holds the child object, not the parent. That's why it cant have the
counter_cache_column and the test:
if @owner[counter_cache_name] and not @owner.new_record?
fails.

I really would appreciate anyone who gives this a try and reports the
results.
The original bug (and a patch) about this issue has been posted about
2 years ago!

_jeanmartin

Michael Koziarski

unread,
Jan 15, 2008, 5:41:55 PM1/15/08
to rubyonra...@googlegroups.com
On Jan 16, 2008 12:42 AM, jeanmartin <jan.sch...@gmx.de> wrote:
>
> Hi,
> I've already filed a bug: http://dev.rubyonrails.org/ticket/10804
> I'm still not sure if I'm missing something:
>
> When I update a child by setting the association method to nil, the
> counter of the parent does not get decremented. For a code example see
> the first answer to my ticket.
>
> The problem seems to be that the @owner variable in
> activerecord/lib/active_record/associations/belongs_to_association.rb
> holds the child object, not the parent. That's why it cant have the
> counter_cache_column and the test:
> if @owner[counter_cache_name] and not @owner.new_record?
> fails.

Without an identity map this is very difficult to fix. The correct
fix for this bug would require full bi-directional has_many and
belongs/to associations, and could take a bit of work.

If someone wants to investigate this, I'd be happy to lend a helping
hand. On the other hand, there are some other changes brewing in that
area now, so perhaps it's worth waiting a bit?

--
Cheers

Koz

jeanmartin

unread,
Jan 16, 2008, 8:08:15 AM1/16/08
to Ruby on Rails: Core
> Without an identity map this is very difficult to fix. The correct
> fix for this bug would require full bi-directional has_many and
> belongs/to associations, and could take a bit of work.
>
> If someone wants to investigate this, I'd be happy to lend a helping
> hand. On the other hand, there are some other changes brewing in that
> area now, so perhaps it's worth waiting a bit?

I just removed @owner[counter_cache_name] from the check. If the
parent does not hold the counter_cache_column then counter_cache_name
would be nil. Since this is tested in the if statement too it should
not be a problem. The AR test suite passes without a failure...
Please give the fix +1
(http://dev.rubyonrails.org/ticket/10804)
_jan
Reply all
Reply to author
Forward
0 new messages