计算儿童的计数范围
我需要一个计算孩子的范围,并将其与另一个数字进行比较。 像这样的东西:
Training_date.rb
scope :not_full, ->{where("subscriptions.count < candidate_limit")}
然后将该范围调用到training_date索引。 只有我得到一个未知的列错误。
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
scope :not_full, { :include => :subscriptions, :conditions => 'subscriptions.count < candidate_limit' }