class Project
has_many :quotes
scope :available, ->(business_id) { joins(:quotes).where.not(quotes: { business_id: business_id }) }
scope :active, ->(business_id) { joins(:quotes).where(quotes: { business_id: business_id }) }
end
class Quote
belongs_to :project
end
嗨,我正在尝试定义一个available
范围,该范围会返回Project
个记录的关系,这些记录对于给定的{{1}不具有Quote
}}。我尝试使用上面的范围,但它返回一个空关系?
类似的business_id
范围似乎工作正常。这里唯一的区别是active
子句。
有什么想法吗?我是否必须为此编写原始SQL?
答案 0 :(得分:2)
应该这样做。
scope :available, ->(business_id) {where.not(Quote.where("quotes.project_id = projects.id and quotes.business_id = ?", business_id).exists)}