2011-10-11 79 views
0

我有以下協會成立Rails的預先​​加載的條件

class bookinghdr 
    belongs_to :agent 
end 

class bookingitem 
    belongs_to :bookinghdr, :include => agent 
end 

所以我期待能做到以下幾點:

named_scope :prepay, :include=>["bookinghdr"], :conditions => ["bookinghdr.agent.agenttype = 'PP'"] 

,並在我的控制器做:

b = Bookingitem.prepay 

但是,這給了我一個ActiveRecord :: StatementInvalid:Mysql ::錯誤:未知列'bookinghdr.agent.agenttyp E」

但是如果我不包括條件子句然後我得到一個記錄上,我可以這樣做:

b = Bookingitem.prepay 
b[0].bookinghdr.agent.agenttype 

沒有任何錯誤!

我不想得到所有記錄,然後遍歷它們以找到代理人擁有'PP @標誌'的記錄。我希望AR能爲我做到這一點。

有人有任何想法如何實現這一目標?

回答

1

你的問題表明你還沒有完全理解關聯和命名範圍是如何工作的。由於我不能從你的問題中知道哪些部分不清楚,我建議你閱讀關聯基礎指南http://guides.rubyonrails.org/v2.3.11/association_basics.html。這應該讓你瞭解你想要實現的概念。閱讀完指南後,應該都是有道理的。