棘手的协会问题Rails

时间:2010-08-03 17:57:29

标签: ruby-on-rails associations polymorphic-associations

#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位用户中的大多数投票者投票的其他投票人?

由于

1 个答案:

答案 0 :(得分:0)

你对多数人的构成并不太具体。这样的事情应该返回由一组用户投票的项目,从最高到最低。您可以添加一个条件,其中vote_count大于多数。

Vote_Item.find(:all, :joins => :votes, 
                     :select => "votes.*, count(votes) vote_count", 
                     :conditions => {:votes => {:user => @users}}, 
                     :group => "vote_items.id", :order => "vote_count desc")
相关问题