unmantained-activeadmin-plugins / activeadmin-globalize

MIT License
51 stars 189 forks source link

Sorting of translated column leads to an exception #41

Closed besi closed 10 years ago

besi commented 10 years ago

I have a model called Programme which has a translated attribute called summary.

When I enable sorting on that column I get an exception:

Mysql2::Error: Unknown column 'summary' in 'order clause': SELECT  `programmes`.* FROM `programmes`   ORDER BY `summary` desc LIMIT 30 OFFSET 0

I tried to sort by translations_summary which leads to a similar issue.

def index do
  column :title, sortable: false # fallback
  column :title, sortable: :title # exception
  column :title, sortable: :translations_title #exception
end

Curiously I can access the summary property of a given programme, but active_admin is not able to sort by this property. Is there a way to include(:translations) like in the following scope-definition example?

default_scope -> { order('programme_translations.title').includes(:translations) }
besi commented 10 years ago

The Active Admin Docs cover this issue:

Associated Sorting

You’re normally able to sort columns alphabetically, but by default you can’t sort by associated objects. Though with a few simple changes, you can.

Assuming you’re on the Books index page, and Book has_one Publisher:

controller do
  def scoped_collection
    resource_class.includes(:publisher) # prevents N+1 queries to your database
  end
end

Then it’s simple to sort by any Publisher attribute from within the index table:

 index do
   column :publisher, sortable: 'publishers.name'
 end