vote_fu . :
user.vote_count
user.vote_count(true)
user.vote_count(false)
posts.votes_count
posts.votes_for
posts.votes_against
posts.votes_total
, :
.
class User < ActiveRecord::Base
has_many :posts
has_many :votes, :through => :posts
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :votes
end
class Vote < ActiveRecord::Base
belongs_to :post
end
1.1) ,
Vote.count
Vote.count(:conditions => {:vote => true})
Vote.count(:conditions => {:vote => false})
1.2)
user.votes.count
user.votes.count(:conditions => {:vote => true})
user.votes.count(:conditions => {:vote => false})
2.1)
User.find(:all, :select => "users.*, count(votes.id) AS count",
:joins => [:posts, :votes],
:conditions => [" votes.vote = ? ", true],
:group => "votes.id", :order => "count DESC")
2.2) .
Post.find(:all, :select => "posts.*, count(votes.id) AS count",
:joins => [:votes],
:conditions => [" votes.vote = ? ", true],
:group => "votes.id", :order => "count DESC")
3.1) . 1.2.1