棘手的关联问题轨道
#Vote Model
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
#votes table
Voteid: integer, vote: boolean, voteable_id: integer, voteable_type: string, voter_id: integer, voter_type: string, created_at: datetime, updated_at: datetime
#User
has_many :voteables, :foreign_key => :voter_id, :class_name => "Vote"
has_many :votes, :foreign_key => :voter_id
#Vote_Item model
acts_as_voteable
has_many :voters, :class_name => "Vote", :foreign_key => :voteable_id
假设有 50 个用户对 VoteItem1 进行了投票,那么找出这 50 个用户中的大多数用户对哪些其他 VoteItem 进行了投票的最佳方法是什么?
谢谢
#Vote Model
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
#votes table
Voteid: integer, vote: boolean, voteable_id: integer, voteable_type: string, voter_id: integer, voter_type: string, created_at: datetime, updated_at: datetime
#User
has_many :voteables, :foreign_key => :voter_id, :class_name => "Vote"
has_many :votes, :foreign_key => :voter_id
#Vote_Item model
acts_as_voteable
has_many :voters, :class_name => "Vote", :foreign_key => :voteable_id
Let's say 50 users have voted on VoteItem1, what's the best way to find out, what other VoteItem(s) a majority of those same 50 users have voted on?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不太具体说明什么构成多数。像这样的东西应该返回由一组用户投票的项目,从最高到最低。您可以添加一个条件,其中 vote_count 大于多数。
You're not too specific about what constitutes a majority. Something like this should return items voted for by a collection of users, from highest to lowest. You could add a condition where vote_count is greater than majority.