2013-08-02 48 views
2

我有一個表單,我希望將新發票與具有外部類型(工作順序belongs_to類型)的工作訂單關聯(類型中的布爾型字段稱爲內部)。Rails設置在簡單表單關聯中使用的範圍

這是代碼,我想工作:

<%= f.association :workorder, :collection => Workorder.external, :label_method => :wonum_desc, :label => 'Work Order' %> 

所以,我試圖建立一個名爲在工作單模型外部範圍。

這給了我 '未定義的方法內部':

scope :external, where(:type.internal => false) 

感謝您的幫助!

回答

1

你必須包括類型的模型,然後在類型表的內部字段中添加一個條件:

scope :external, includes(:type).where(types: { internal: false }) 
# notice the syntax:  ^^^^  ^^^^^ 
# in includes/joins, use the relation's name (here, Workoder belongs_to :type) 
# in where, use the table's name (usually the pluralized version of the relation) 
+0

感謝您的回答! – Reddirt