• Aug 13 2019

RoR – Active Admin Filters on Attributes of Associated Models

Written by - Team Simple Logic

In one of our application, there was a requirement to have filters on attributes of associated models. We use ActiveAdmin and it provides a nice feature to satisfy this requirement with the help of Ransack(https://github.com/activerecord-hackery/ransack) gem. Let’s consider following example: class Order < ActiveRecord::Base belongs_to :customer has_many :order_products end class OrderProduct < ActiveRecord::Base belongs_to :order end Now, we need to have a filter for ‘order_date’ field of Order table on the OrderProduct index page. And We can simply achieve this with the following: ActiveAdmin.register OrderProduct do filter :order_order_date, as: :date_range end Also, we can have filters for attributes of associations of association as follows. For eg: If we need to have a filter for ‘account_code’ field of Customer table which is associated with the Order table then we can do the following: filter :order_customer_account_code, as: :string


Leave a Comment

Contact Us!