如何为多态关联添加过滤器?

时间:2015-08-10 08:25:47

标签: ruby-on-rails ruby activeadmin

我需要在ActiveAdmin中按字符串添加搜索。 类似的东西:

ActiveAdmin.register Comment do
 filter :post_title, as: :string
end

如果我有这样的关联:

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
end

class Post < ActiveRecord::Base
 belongs_to :content, polymorphic: true
 has_many :comments, as: :commentable
end

如果我有两个多态关联,我该怎么做?有必要创建自定义过滤器吗?

1 个答案:

答案 0 :(得分:2)

您可能必须执行类似的操作

ActiveAdmin.register Comment do
 filter :commentable_of_Post_type_title, as: :string
end

请注意,of_Post_type用大写的P,它有助于Ransack在生成的SQL查询中正确指定表名。

title中的Post_type_titlePosts表的属性。您可以用其他任何东西代替它。

关于Ransacking多态关联,您可以在此处从Ransack Wiki阅读更多信息:https://github.com/activerecord-hackery/ransack/wiki/Polymorphic-searches