我正在使用Thumbs_up創建投票功能的gem。我有3個表格 - 發佈,用戶和投票,郵政是acts_as_voteable和用戶是acts_as_voter。如何在Ruby on Rails中獲取未投票的帖子數
型號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
型號Vote.rb
class Vote < ActiveRecord::Base
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND 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
型號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
現在我想算的職位數量的,沒有投票。我試圖這樣做..
<%= Post.joins(:votes).where("dont_know_how_to_write_condition").count %>
任何幫助?在此先感謝
這是MySQL或PostgreSQL? – Lucas 2013-05-10 08:29:26
在開發模式我使用的是SQLite和在生產模式下我使用postgresql – SoftwareGeek 2013-05-10 08:33:30