简体   繁体   中英

Adding filter for has_many relationship in rails_admin

I'm using rails_admin as a reporting tool as well and leveraging the export to CSV feature to pull out specific data. This works fine, but I need to now filter results based on data being between 2 dates, but the dates are on on a model that is linked through a has_many relationship.

Here is an example

class Patient < ActiveRecord::Base
  has_many :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :patient
  # has field: 'date'
end

I can load patients in RailsAdmin and in the list view show the appointments, but how can I add a filter to only show patients that have an appointment between Date X and Y? Is this possible at all?

Is there anyway to use named scope and pass in parameters? Or what about a virtual model for rails_admin to load? The SQL is pretty simple for this, but I'm just not sure how to pass in the actual date parameters?

I am also curious as to how to solve this.

One option is to add appointments to filter dropdown but it will not allow you to choose dates.

rails_admin do
  include_all_fields
  field :appointments do
    filterable true
  end
  list do
    filters [:appointments] # this will make it show up by default
  end
end

Another choice is to create custom action and create page at app/views/rails_admin/mail/filter.html.haml where you can build regular search form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM