This project is archived and is in readonly mode.

counter_cache fails silently in rails 3
Reported by bdtomlin | February 14th, 2010 @ 12:31 AM
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">
Comments and changes to this ticket
- 
            
         bdtomlin February 14th, 2010 @ 12:35 AM- Tag set to associations, belongs_to, counter_cache, rails3
 
- 
         
- 
            
         bdtomlin February 17th, 2010 @ 04:56 AMIt 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. 
- 
         Ryan Bigg October 11th, 2010 @ 10:55 AM- Tag cleared.
- Importance changed from  to Low
 Automatic cleanup of spam. 
- 
         
- 
         Santiago Pastorino February 2nd, 2011 @ 04:48 PM- 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. 
- 
         Santiago Pastorino February 2nd, 2011 @ 04:48 PM- State changed from open to stale
 
- 
            
         csnk May 18th, 2011 @ 08:27 AMWe are the professional underwear manufacturer, underwear supplier, underwear factory, custom underwear. 
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
 bdtomlin
      bdtomlin
 Jeremy Kemper
      Jeremy Kemper
 José Valim
      José Valim
 Ryan Bigg
      Ryan Bigg