我需要一个对孩子进行计数的范围,并将其与另一个数字进行比较。像这样:
Training_date.rb
scope :not_full, ->{where("subscriptions.count < candidate_limit")}
然后将该范围调用到training_date索引。只有我得到一个未知的列错误。
答案 0 :(得分:2)
scope :not_full, ->{ where("subscriptions.count < candidate_limit") }
除非您加入subscriptions
并且count
是subscriptions
中的列,否则无效。但我认为以下是你想要的:
scope :not_full, -> do
where("
training_dates.candidate_limit > (
SELECT COUNT(*) FROM subscriptions WHERE (
subscriptions.training_date_id = training_dates.id
)
)
")
end
通过正确的关联,以下内容也可以起作用:
scope :not_full, -> do
joins(:subscriptions).
group("training_dates.id").
having("training_dates.candidate_limit > COUNT(subscriptions.id)")
end
答案 1 :(得分:0)
scope :not_full, { :include => :subscriptions, :conditions => 'subscriptions.count < candidate_limit' }