我有资源订单。在我的管理面板 (activeadmin) 中,我需要一个 bool 过滤器来获取用户具有特定角色的所有订单。
在 Order 类中,我有一个这样的范围:
class Order
belongs_to :user
scope :client_only_in, -> { joins(:user).where('users.role = ?', 'client') }
end
在事件管理 order.rb 中,我添加了以下过滤器:
filter :client_only, as: :check_boxes
使用 rails 3(元搜索)我可以添加
search_method :client_only_in, type: :boolean
获得过滤器,但使用 rails 4 和 Ransack 我不知道如何做到这一点。
不添加
search_method
到订单模型我收到错误undefined method `client_only_in' for Ransack::Search
当我访问索引管理订单页面时。
有什么帮助吗?
最佳答案
我不知道有什么方法可以做到这一点,但您始终可以有一个选择字段来根据用户角色进行过滤:
filter :users_role, as: :select, multiple: true, collection: proc{ User.uniq.pluck :role }
更新:啊,如果有订单
belongs_to
一个用户比它应该是 :user_role
而不是 :users_role
关于ruby-on-rails - 带有 Ransack 的 Active Admin Rails4 自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20642297/