2015-09-07 76 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

我看到真正相關的!這有用,歡呼! –

相關問題