Model association question

前端 未结 2 573
孤街浪徒
孤街浪徒 2021-01-15 23:19

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 vote_count, and vote_cou

2条回答
  •  清歌不尽
    2021-01-15 23:36

    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.

提交回复
热议问题