模型关联问题 [英] Model association question

查看:60
本文介绍了模型关联问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在实现上/下投票机制,并为此生成了一个模型.到目前为止,我了解到一个视频(将进行投票的视频)具有一个vote_count,而vote_count属于视频.但是,我也想在我的poll_count数据库中跟踪对视频进行投票的用户.这是否意味着vote_count拥有许多用户,并且该用户属于一个vote_count?

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_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.

这篇关于模型关联问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆