0
#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 的最佳方法是什么?

谢谢

4

1 回答 1

0

您对什么构成多数并不太具体。像这样的东西应该返回由一组用户投票的项目,从最高到最低。您可以添加一个投票计数大于多数的条件。

Vote_Item.find(:all, :joins => :votes, 
                     :select => "votes.*, count(votes) vote_count", 
                     :conditions => {:votes => {:user => @users}}, 
                     :group => "vote_items.id", :order => "vote_count desc")
于 2010-08-03T19:07:37.863 回答