使用Ransack生成自定义Active Admin Filter

时间:2016-01-21 08:51:58

标签: ruby ruby-on-rails-4 activeadmin ransack

我希望创建一个自定义过滤器,根据我设置的关联搜索其他表格数据,我已在我的视图中实现了这一点,但无法将其传输到带有Ransack的自定义过滤器。

我正在尝试搜索Tournament name

我有以下3个模型和关联设置:

class Gallery < ActiveRecord::Base
  belongs_to :tournament
  has_many :gallery_images, dependent: :destroy
  accepts_nested_attributes_for :gallery_images, allow_destroy: :true
end

class Tournament < ActiveRecord::Base
  has_many :fixtures, dependent: :destroy
  has_one :gallery, dependent: :destroy
  has_many :gallery_images, through: :gallery, dependent: :destroy
end

class GalleryImage < ActiveRecord::Base
  belongs_to :gallery
  has_attached_file :photo
end

在我看来,我已经实现了这个目标:

ActiveAdmin.register GalleryImage do
  index do
    selectable_column
    column 'Tournament' do |gal|
      tourn = gal.gallery
      tourn.tournament.name
    end
  end
end

如何使用Ransack将其转换为查询?

filter :custom_ransacker_method
       as: :select,
       collection: -> { Tournament.pluck(:name, :id) },
       label: 'Tournament Name'

我不确定如何构建自定义的ransacker方法。

0 个答案:

没有答案