2011-10-14 40 views
3

我使用的是Active Admin 0.3.2,而我的数据库模式包含一些has_and_belongs_to_many关系。我可以创建并显示记录,但是试图将它们包含在筛选器部分中会导致事情停止。如何在Active Admin过滤器部分使用HABTM字段?

型号\ pin.rb:

class Pin < ActiveRecord::Base 
    has_and_belongs_to_many :pin_types, :join_table => :pin_types_pins 
end 

型号\ pin_type.rb

class PinType < ActiveRecord::Base 
    has_and_belongs_to_many :pins, :join_table => :pin_types_pins 
end 

管理\ pins.rb

ActiveAdmin.register Pin do 
    filter :pin_types 
    ...other filters 
end 

结果是其他过滤器的出现,但针类型没有任何部分。

如果管理员\ 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> 

我希望做的是让用户选择一个或多个引脚根据是否应用任何选定选项,从一组可能的选项和过滤器中筛选类型。

这可能吗?

+1

我自己找到了解决方案,[在GitHub上](https://github.com/gregbell/active_admin/issues/515)。 供参考:'filter:pin_types_id,:as =>:check_boxes,:collection => proc {PinType.all}' – ccarlson

+0

如果我们想从pin创建Pintype,那么我们需要做什么? –

回答

3

使用这样的过滤器: 过滤:model_attribute ,所以如果你在User_events,想上的用户名搜索,u'd做到这一点 过滤器:user_fullName

+0

优秀!谢谢,rodrigo。 – ccarlson

+0

你能详细解释一下吗?我不确定你是什么意思。 –

0

注意ccarlson的答案作品,但仅限于:check_boxes

这很不幸,因为meta_search的默认行为(用于为搜索过滤器供电)不会过滤出重复项......并且当您使用复选框时,您可能不希望看到结果出现两次,因为它匹配2个选定的选项。

尝试使用as: :select时遇到未定义的方法错误,但必须在复选框上咬住子弹。

相关问题