开发者

Model association question

开发者 https://www.devze.com 2023-02-17 17:01 出处:网络
So I\'m implementing an up/down voting mechanism for which I\'m generating a model. So far I understand that a video (what will be voted on) has one vo开发者_如何学JAVAte_count, and vote_count belongs

So I'm implementing an up/down voting mechanism for which I'm generating a model. So far I understand that a video (what will be voted on) has one vo开发者_如何学JAVAte_count, and vote_count belongs to videos. However, I also want to track in my vote_count database the user that has voted on the video. Does that mean that a vote_count has many users, and that a user belongs to a vote_count?


It may be easier to track the votes as independent records, such as this:

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

If you have people voting up and down, you will need to track the net vote total somehow. This can be tricky, so you may want to look for a voting plugin.


Am I missing something here? Why not assign a netVoteTally as a property of Videos. Initialize it to zero when video.new is called and have incNetVideoTally and decNetVideoTally methods that are accessible outside the video method? Just my $0.02.

0

精彩评论

暂无评论...
验证码 换一张
取 消