2013-03-03 30 views
0

我的模式是這樣的:寫作範圍,加入和秩序的示範

計劃has_many措施 然後 措施has_many目標目標表中有一個名爲列我的查詢是這樣的:

@programs2 = Program.includes([measures: :targets]) 
       .some_scope 
       .where('organization_id = 1') 
       .limit(2) 

我不知道在哪裏或如何編寫some_scope部分查詢。 查詢以Program.includes開頭,因此我認爲它應該在Program模型中進行定義,但是我的問題是measures: :targets。我如何爲他們定義聯合。如果它只是一個表,我知道我可以這樣做:

scope :salary, :joins => :registry, :order => "money DESC" 

我需要這一個了類似的事情,但是這兩個表像我上面所解釋的。我不知道該怎麼寫。

+0

請停止[欺騙](http://stackoverflow.com/questions/15183785/scope-that-has-three-levels-deep-joins)你的問題。只要有人能夠回答,答案就會出現。 – mudasobwa 2013-03-03 09:35:41

+0

@mudasobwa:對不起,我試圖從不同的觀點或方式來解答同一個問題,我可以解釋它。我認爲這一個是最明確和解釋版本的問題,我有tho :) – DarkNightFan 2013-03-03 09:42:50

回答

1

嘗試,

scope :some_scope, joins(mesures: :targets) 
+0

謝謝,但我得到這個錯誤:PG ::錯誤:錯誤:零長度分隔標識符在或接近「」「」 – DarkNightFan 2013-03-03 16:47:06

+0

是嗎?如此糟糕,我放棄並用這種方式編碼: Program.joins(度量::目標).where(organization_id:params [:id])。limit(2)? – DarkNightFan 2013-03-03 16:48:43

+0

是的,除非你試圖訪問度量和目標的內容,否則它會更好。 – 2013-03-03 16:51:30