rails_admin - 基于object_label_method自定义可搜索的过滤器

时间:2015-01-19 14:32:51

标签: ruby-on-rails ruby-on-rails-4 rails-admin

我正在为练习建立某种评分系统。在rails_admin.rb中我有:

config.model Student do
    object_label_method do
      :student_code
    end
end

config.model Grade do
    list do
      field :student do
        searchable :student_code
      end
      field :course
      field :category
      field :score
    end
end

使用rails_admin会自动为我提供学生过滤器,但是会根据student_id进行搜索。 如何在Grades管理视图中自定义学生过滤器以根据student_code属性搜索学生?使用像所建议的文档一样可搜索的结果并没有给出任何结果。

1 个答案:

答案 0 :(得分:0)

我不确定模型StudentGrade之间的关系,但是我有一个模型ConfirmationLetter,我想在其中搜索列{{1} },这是一个:hospitals关联,但未获得任何结果。当我终于找到this answer时,我对通过DSL解决问题失去了希望,所以谢谢@vinothini和@ jxpx777!我确认以下内容仍适用于RA 2.0.0.beta:

在confirmation_letter.rb中:

has_many_through

在rails_admin.rb中:

class ConfirmationLetter < ApplicationRecord
  has_many :bookings, dependent: :nullify, inverse_of: :confirmation_letter
  ...
  has_many :hospitals, through: :bookings
  accepts_nested_attributes_for :hospitals
  ...
end

如果您的object_label_method是RailsAdmin.config do |config| # Some other configuration here config.model 'ConfirmationLetter' do # Some other configuration here configure :hospitals do ... searchable [{Hospital => :name}] sortable true filterable true queryable true end list do include_fields :hospitals, .... end end end 模型的一列,那么我相信您应该能够通过类似于以下内容的学生代码来搜索成绩列表:

Student

这也适用于config.model 'Grade' do configure :students do ... searchable [{Student => :student_code}] sortable true filterable true queryable true end list do include_fields :hospitals, .... end end 关联列。我知道这已经发布了4年了,但是我决定在这里分享我的发现,以防有人像我一样绝望地尝试一下!