Catch Me HuntEr
Catch Me HuntEr

Reputation: 101

Customizing Index in Active Admin

I want to customize index page like display details based on id. For example i have two tables like merchant and customer

merchant.rb

class Merchant < ActiveRecord::Base 
    has_many :customers
end

customer.rb

class Customer < ActiveRecord::Base
    belongs_to :merchant
end

Present my code is like

app/admin/merchant.rb

index  do
  selectable_column
  column :merchant_id
  column :name
  actions   
end

It displays the Merchant Id and Merchant name, on click merchant_id it should show the related customers. Give some ideas to implement

Upvotes: 0

Views: 455

Answers (1)

ahmacleod
ahmacleod

Reputation: 4310

It wouldn't be practical to show all customers for each merchant in a single row, so usually I would prefer to show a customer count and link it to the customer index with merchant filter set.

ActiveAdmin.register Merchant do
  def scoped_collection
    super.select('merchants.*, COUNT(*) AS customer_count')
         .joins(:customers)
         .group('merchants.id')
  end

  index do
    selectable_column
    id_column

    column :customer_count, sortable: 'customer_count' do |merchant|
      link_to merchant.attributes['customer_count'],
              admin_customers_path(q: { merchant_id_eq: merchant.id })
    end

    actions
  end
end

Upvotes: 1

Related Questions