I try to add counter_cache to User to count owned Projects.
User has many projects, Project has many Users, but Project belongs to one of them.
So i wrote migration for adding extra count fields for counter_cache.
Comments has no trouble with reset_counter
.
But User.reset_counters user.id, :projects
raise NoMethodError: undefined method `counter_cache_column' for nil:NilClass.
Also counter_cache on projects is working OK. If i add extra project it will increment projects_count field, if i delete one - decrement.
Models
class User < ActiveRecord::Base
has_many :projects, :through => :team_members
has_many :team_members
class TeamMember < ActiveRecord::Base
belongs_to :user
belongs_to :project
class Project < ActiveRecord::Base
has_many :users, :through => :team_members
has_many :team_members
belongs_to :leader, :class_name => :User, :counter_cache => true
class Comment < ActiveRecord::Base
belongs_to :user, :counter_cache => true
Migration
class AddCounters < ActiveRecord::Migration
def up
add_column :users, :comments_count, :integer, :default => 0
add_column :users, :projects_count, :integer, :default => 0
User.reset_column_information
User.find_each do |user|
User.reset_counters user.id, :comments #all fine
User.reset_counters user.id, :projects #raise NoMethodError
#undefined method `counter_cache_column'
#for nil:NilClass
end
end
def down
remove_column :users, :projects_count
remove_column :users, :comments_count
end
end
i tried to switch from :counter_cache => true
to :counter_cache => :projects_count
.
Same thing.