繁体   English   中英

Rails 5 /简单表单-过滤器关联

[英]Rails 5 / Simple form - filter association

在我的表单中,我有一个关联,我希望将可能的选择减少到活动的关联对象的那些记录。 根据我阅读的简单表格,我使用:

<%= f.association :documenttype, collection: Documenttype.active.order(:name) %>

退货

未定义的方法'active'...

我究竟做错了什么

您需要在模型上具有作用域。

class Documenttype < ActiveRecord::Base
  scope :active, -> { where(active: true) }

假设您的表中有一个布尔列,名为active 如果以其他方式确定它,则可以根据需要修改scope ,例如,也许您有一个status字符串列,该列必须具有字符串“ approved”,因此您将...

class Documenttype < ActiveRecord::Base
  scope :active, -> { where(status: 'approved') }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM