Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

counter_cache not updating on update_attributes

#3903

When trying to use a simple case of cache_counter:

  class Registration < ActiveRecord::Base
    belongs_to :section, :counter_cache => :registrations_count
  end

  class Section < ActiveRecord::Base
    has_many :registrations
  end

and i wanted to test the scenario where i change the section id on a registration record, if the old one's counter cache will -1 and the new one will +1.

here's the test:

  def test_registration_update_changes_section_counts_when_update_attributes
    sec1 = Section.create! :name => "foo"
    sec2 = Section.create! :name => "bar"
    
    r = Registration.create! :name => "foo", :section => sec1
    sec1.reload
    puts "Section 1 registrations: " + sec1.registrations_count.to_s
  
    assert_equal 1, sec1.registrations_count
    
    r.update_attributes(:section_id => sec2) 
    sec1.reload
    puts "Section 1 after reduce: " + sec1.reload.registrations_count.to_s
    assert_equal 0, sec1.registrations_count
    sec2.reload
    assert_equal 1, sec2.registrations_count
  end

which fails.

The patch applies on 2-3-stable (pulled today) but i am uncertain it's the right way to solve it. i'll wait for the discussion here and create a master patch based on comments.

Reported by Elad Meidar · February 9th, 2010 @ 04:56 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg
    • State changed from new to open
    • Importance changed from to Low

    Hey Elad,

    Is this now fixed in 2-3-stable? Could you please update this ticket with your findings? Thanks.

    (yay for delegation)

    October 11th, 2010 @ 01:02 AM

  2. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 02:59 AM

  3. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:24 AM

  4. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 26th, 2011 @ 12:00 AM

  5. rails
    rails
    • State changed from open to stale

    February 26th, 2011 @ 12:00 AM