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 に答える