2015-09-07 86 views
0
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记录的关系不具有对给定business_id一个Quote。我试过使用上面的范围,但它返回一个空关系?查找记录,其中记录的关联属性都不是一个定值

类似的active范围似乎工作正常。这里唯一的区别是.not()条款。

任何想法?我必须为此编写原始SQL吗?

+0

http://stackoverflow.com/a/31256399/2697183 – AbM

+0

@AbM这并不是问题 –

回答

1

这应该这样做。

scope :available, ->(business_id) {where.not(Quote.where("quotes.project_id = projects.id and quotes.business_id = ?", business_id).exists)} 
+0

我看到真正相关的!这有用,欢呼! –

相关问题