How to use HABTM fields in Active Admin filter section?
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' 开发者_如何学Cfor #<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?
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
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.
精彩评论