Model association question

前端 未结 2 493
孤街浪徒
孤街浪徒 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.

    0 讨论(0)
  • 2021-01-15 23:54

    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 讨论(0)
提交回复
热议问题