Problem with counter_cache implementation

你离开我真会死。 提交于 2019-12-03 00:53:08

问题


I'm getting 'rake aborted! ... posts_count is marked readonly' errors.

I have two models: user and post.

users has_many posts.

posts belongs_to :user, :counter_cache => true

I have a migration which adds the posts_count column to the users table and then calculates and records the current number of posts per user.

self.up
  add_column :users, :posts_count, :integer, :default => 0

  User.reset_column_information
  User.all.each do |u|
    u.update_attribute( :posts_count, u.posts.count)
  end
end

when I run the migration I get the error. This is pretty clear-cut, of course and if I remove the :counter_cache declaration from the posts model, e.g.

belongs_to :user

the migration runs fine. This obviously, does not make sense because you couldn't really implement it this way. What am I missing?


回答1:


You should be using User.reset_counters to do this. Additionally, I would recommend using find_each instead of each because it will iterate the collection in batches instead of all at once.

self.up
  add_column :users, :posts_count, :integer, :default => 0

  User.reset_column_information
  User.find_each do |u|
    User.reset_counters u.id, :posts
  end
end



回答2:


OK, the documentation states:

Counter cache columns are added to the containing model’s list of read-only attributes through attr_readonly.

I think this is what happens: you declare the counter in the model's definition, thus rendering the "posts_count" attribute read-only. Then, in the migration, you attempt to update it directly, resulting in the error you mention.

The quick-and-dirty solution is to remove the counter_cache declaration from the model, run the migration (in order to add the required column to the database AND populate it with the current post counts), and then re-add the counter_cache declaration to the model. Should work but is nasty and requires manual intervention during the migration - not a good idea.

I found this blog post which suggests altering the model's list of read-only attributes during the migration, it's a bit oudated but you might want to give it a try.



来源:https://stackoverflow.com/questions/4295404/problem-with-counter-cache-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!