It works:
class Fact < ActiveRecord::Base
scope :by_user, lambda { |id| joins(:user).where('users.id == ?', id).readonly(false) }
scope :vote_count, lambda { |count| where('? == (select count(fact_id) from votes where votes.fact_id == facts.id)', count)}
end
Fact.by_user(1).vote_count(0)
The vote_count scope is a bit sqly, but you can link these crawlers as you like, you can also see the underlying sql:
Fact.by_user(1).vote_count(0).to_sql
, :
f = Arel::Table.new(:facts)
v = Arel::Table.new(:votes)
u = Arel::Table.new(:users)
sql.
sql = f.join(u).on(f[:user_id].eq(1)).where('0 == (select count(fact_id) from votes where votes.fact_id == facts.id)').to_sql
: f[:user_id].eq(1)
, :
Fact.find_by_sql(sql)
, , , ( " 0 ==..." ). , Rails3 Arel - http://m.onkey.org/active-record-query-interface