如何使用活动管理过滤器部分中的 HABTM 字段?
我使用的是 Active Admin 0.3.2,我的数据库架构包括一些 has_and_belongs_to_many 关系。我可以很好地创建和显示记录,但尝试将它们包含在过滤器部分中会导致事情陷入停滞。
models\pin.rb:
class Pin < ActiveRecord::Base
has_and_belongs_to_many :pin_types, :join_table => :pin_types_pins
end
models\pin_type.rb
class PinType < ActiveRecord::Base
has_and_belongs_to_many :pins, :join_table => :pin_types_pins
end
admin\pins.rb
ActiveAdmin.register Pin do
filter :pin_types
...other filters
end
结果是出现其他过滤器,但根本没有 Pin Types 部分。
如果 admin\pins.rb 是这样的:
ActiveAdmin.register Pin do
filter :pin_types, :as => :check_boxes
...other filters
end
我得到以下信息:
undefined method `pin_type_ids_in' for #<MetaSearch::Searches::Pin:0xcd2c108>
我想做的是允许用户从一组可能的选择中选择一个或多个 Pin 类型,并根据是否应用任何选定的选项进行过滤。
这可能吗?
I'm using Active Admin 0.3.2, and my database schema includes a few has_and_belongs_to_many relationships. I can create and display records just fine, but attempting to include them in the filter section causes things to grind to a halt.
models\pin.rb:
class Pin < ActiveRecord::Base
has_and_belongs_to_many :pin_types, :join_table => :pin_types_pins
end
models\pin_type.rb
class PinType < ActiveRecord::Base
has_and_belongs_to_many :pins, :join_table => :pin_types_pins
end
admin\pins.rb
ActiveAdmin.register Pin do
filter :pin_types
...other filters
end
The result is that the other filters appear, but there's no section at all for Pin Types.
If admin\pins.rb is this instead:
ActiveAdmin.register Pin do
filter :pin_types, :as => :check_boxes
...other filters
end
I get the following:
undefined method `pin_type_ids_in' for #<MetaSearch::Searches::Pin:0xcd2c108>
What I'd like to do is allow the user to select one or many Pin Types from a set of possible choices and filter depending on whether any of the selected options apply.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样使用过滤器:
过滤器:模型属性
,所以如果您在 User_events 上并且想要搜索用户名,您可以这样做
过滤器:用户全名
use the filter like this:
filter :model_attribute
, so if you are on the User_events and wanted to search on the user name , u'd do this
filter :user_fullName
请注意,ccrlson 的答案有效,但仅适用于
:check_boxes
。这是不幸的,因为meta_search(用于支持过滤器搜索)的默认行为不会过滤掉重复项...并且当您使用复选框时,您可能不希望看到结果出现两次,因为它匹配2 个选定的选项。
当我尝试使用
as: :select
时,遇到了未定义的方法错误,但不得不硬着头皮选择复选框。Note that ccarlson's answer works, but only with
:check_boxes
.This is unfortunate, because the default behavior of meta_search (used to power the filter searching) does not filter out duplicates... and when you're using checkboxes, you probably don't want to see a result come up twice because it matches 2 selected options.
I ran into the undefined method error here when attempting to use
as: :select
, but had to bite the bullet on checkboxes.