how to get unvoted post count in Ruby on Rails -
i using thumbs_up gem creating vote function. have 3 tables - post, user , vote post acts_as_voteable , user acts_as_voter.
model post.rb
class post < activerecord::base attr_accessible :title, :content, :user_type acts_as_voteable validates_presence_of :title,:content default_scope order: 'posts.created_at desc' end
model vote.rb
class vote < activerecord::base scope :for_voter, lambda { |*args| where(["voter_id = ? , voter_type = ?", args.first.id, args.first.class.base_class.name]) } scope :for_voteable, lambda { |*args| where(["voteable_id = ? , voteable_type = ?", args.first.id, args.first.class.base_class.name]) } scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) } scope :descending, order("created_at desc") belongs_to :voteable, :polymorphic => true belongs_to :voter, :polymorphic => true attr_accessible :vote, :voter, :voteable end
model user.rb
class user < activerecord::base attr_accessible :name, :email, :password, :password_confirmation has_secure_password acts_as_voter has_many :posts, dependent: :destroy end
now want count posts number not voted. trying this..
<%= post.joins(:votes).where("dont_know_how_to_write_condition").count %>
any help?? in advance
i not sure if work in sqlite, should on postgresql though.
def self.not_voted joins("left outer join votes on votes.voteable_id = posts.id"). where("votes.id null") end
then:
post.not_voted.count
Comments
Post a Comment