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 fails silently in rails 3

#3954

Here is the code for the simplest possible implementation:

  rails g model Collection name:string collectibles_count:integer
  rails g model Collectible name:string collection:references

edit the CreateCollections migration for default value

  t.integer :collectibles_count, :default => 0

The model code

  class Collection < ActiveRecord::Base
    has_many :collectibles
  end

  class Collectible < ActiveRecord::Base
    belongs_to :collection, :counter_cache => true
  end

and in the rails console...

  collection = Collection.create(:name => 'collection name')
  => #<Collection id: 2, name: "collection name", collectibles_count: 0, created_at: "2010-02-14 00:20:40", updated_at: "2010-02-14 00:20:40">


collectible = Collectible.create(:name => 'collectible name') => #<Collectible id: 3, name: "collectible name", collection_id: nil, created_at: "2010-02-14 00:21:16", updated_at: "2010-02-14 00:21:16">


collection.collectibles << collectible => [#<Collectible id: 3, name: "collectible name", collection_id: 2, created_at: "2010-02-14 00:21:16", updated_at: "2010-02-14 00:21:52">]


collection => #<Collection id: 2, name: "collection name", collectibles_count: 0, created_at: "2010-02-14 00:20:40", updated_at: "2010-02-14 00:20:40">


collection.collectibles.create(:name => 'another collectible') => #<Collectible id: 4, name: "another collectible", collection_id: 2, created_at: "2010-02-14 00:22:40", updated_at: "2010-02-14 00:22:40">


collection => #<Collection id: 2, name: "collection name", collectibles_count: 0, created_at: "2010-02-14 00:20:40", updated_at: "2010-02-14 00:20:40">

Reported by bdtomlin · February 14th, 2010 @ 12:31 AM

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

Activity

  1. bdtomlin
    bdtomlin
    • Tag set to associations, belongs_to, counter_cache, rails3

    February 14th, 2010 @ 12:35 AM

  2. José Valim
    José Valim

    Could you please provide a failing test case?

    February 16th, 2010 @ 09:45 PM

  3. bdtomlin
    bdtomlin

    It turns out that this is only in one case of using the << method to add a relation. I am new to this, so I hope this is what you are asking for. I will attach a diff of the new failing test case.

    February 17th, 2010 @ 04:56 AM

  4. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:55 AM

  5. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:26 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    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 2nd, 2011 @ 04:48 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:48 PM