Jus*_*zer 5 ruby ruby-on-rails associations counter-cache ruby-on-rails-3
Unknown key(s): counter_cache尝试在我的RoR应用程序中实现计数器缓存列时出现错误.
我在这个问题中实现了模型关联:模型关联问题
这是我的迁移:
class AddVideoVotesCountToVideos < ActiveRecord::Migration
def self.up
add_column :videos, :video_votes_count, :integer, :default => 0
Video.reset_column_information
Video.find(:all).each do |p|
p.update_attributes :videos_votes_count, p.video_votes.length
end
end
def self.down
remove_column :videos, :video_votes_count
end
end
Run Code Online (Sandbox Code Playgroud)
然而,在看了http://media.railscasts.com/videos/023_counter_cache_column.mov后,我想也许我不得不在:counter_cache => true之后进入VideoVote模型belongs_to :video.但是,当我这样做时,我收到错误:
wrong number of arguments (2 for 1)
我究竟做错了什么?
update_attribute不是update_attribteS
p.update_attribute :videos_votes_count, p.video_votes.length
Run Code Online (Sandbox Code Playgroud)
或与update_attributes:
p.update_attributes( :video_votes_count => p.video_votes.length )
Run Code Online (Sandbox Code Playgroud)
更新1
:counter_cache => true应该在 VideoVote 类中:
class VideoVote < ActiveRecord::Base
belongs_to :user
belongs_to :video, :counter_cache => true
end
Run Code Online (Sandbox Code Playgroud)