Jus*_*zer 1 ruby model ruby-on-rails ruby-on-rails-3
所以我正在实现一个上/下投票机制,我正在生成一个模型.到目前为止,我知道视频(将被投票的内容)有一个vote_count,而vote_count属于视频.但是,我还想在我的vote_count数据库中跟踪投票的用户.这是否意味着vote_count有很多用户,而用户属于vote_count?
跟踪投票作为独立记录可能更容易,例如:
class VideoVote < ActiveRecord::Base
belongs_to :user
belongs_to :video
end
class User < ActiveRecord::Base
has_many :video_votes
has_many :voted_videos,
:through => :video_votes,
:source => :video
end
class Video < ActiveRecord::Base
has_many :video_votes,
:counter_cache => true
has_many :voted_users,
:through => :video_votes,
:source => :user
end
Run Code Online (Sandbox Code Playgroud)
如果你有人投票和投票,你需要以某种方式跟踪净投票总数.这可能很棘手,因此您可能想要查找投票插件.