gregbell/active_admin — Issue #811: Sort by has_one
Author: fuse
Created: 2011-12-03 16:21:07+00:00
State: open

Hey guys,

My need is really close to https://github.com/gregbell/active_admin/pull/623, I would like to be able to sort items with an has_one relation.

Actually I'm using globalize and would like to sort items by translated fields.

class LearningSession < ActiveRecord::Base
has_one :learning_session_translation, :class_name => "LearningSession::Translation", :foreign_key => "learning_session_id"
end

when using

scope :joined, :default => true do |learning_session|
learning_session.includes [:learning_session_translation]
end

and

column :label, :sortable => :'learning_session_translations.label'

it works, but the items are not ordered, it produces some sql like this:

SELECT `learning_sessions`.`id` AS t0_r0, … `learning_session_translations`.`id` AS t1_r0, … FROM `learning_sessions` LEFT OUTER JOIN `learning_session_translations` ON `learning_session_translations`.`learning_session_id` = `learning_sessions`.`id` ORDER BY learning_session_translations.label desc

Any tips or pointers ?