Rails's Administrate - Sorting records in show view

45 Views Asked by At

I have models named Conference, Attend and Delegate. A Conference has many Delegates through Attend (Attend is used as a join table).

In conference_dashboard.rb:

ATTRIBUTE_TYPES = {
    id: Field::Number,
    attends: Field::HasMany 
  }.freeze

SHOW_PAGE_ATTRIBUTES = %i[
    attends
  ].freeze

In attend_dashboard.rb:

ATTRIBUTE_TYPES = {
    id: Field::Number,
    delegates: Field::BelongsTo,
    attended: Field::Boolean
  }.freeze

COLLECTION_ATTRIBUTES = %i[
    delegate
    attended
  ].freeze

Conference's show view looks like below: enter image description here

The question is: When I click on the sort link (highlighted in yellow), I want Attend records will be sorted based on delegates.name, not delegates.id (default). How can I do that?

1

There are 1 best solutions below

0
On

You can find the documentation for what you are looking for over here: https://administrate-demo.herokuapp.com/customizing_dashboards

It is possible to customize the dashboard and give options to it. In your case you need to update the attend_dashboard.rb to the following:

ATTRIBUTE_TYPES = {
    id: Field::Number,
    delegates: Field::BelongsTo.with_options(
        order: ['name']
    ),
    attended: Field::Boolean
  }.freeze

COLLECTION_ATTRIBUTES = %i[
    delegate
    attended
  ].freeze