3

我有一个表单,我想将新发票与具有外部类型(Workorder belongs_to Type)的工作订单相关联(类型中的布尔字段称为内部)。

这是我想要工作的代码:

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

所以,我试图在工作订单模型中设置一个名为 external 的范围。

这给了我“内部未定义的方法”:

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

谢谢您的帮助!

4

1 回答 1

1

您必须包含 Type 模型,然后在 Type 表的内部字段上添加条件:

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)
于 2013-08-02T15:40:53.307 回答