From 3453a8eee784b84b9e29baf4a89d0476cae28979 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 13 Feb 2020 20:46:11 +0000 Subject: [PATCH] Don't assume the foreign key will be ${FOO}_id --- lib/administrate/order.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/administrate/order.rb b/lib/administrate/order.rb index 4f32da6cd2..25f9d19602 100644 --- a/lib/administrate/order.rb +++ b/lib/administrate/order.rb @@ -62,7 +62,7 @@ def order_by_count(relation) end def order_by_id(relation) - relation.reorder("#{attribute}_id #{direction}") + relation.reorder("#{foreign_key(relation)} #{direction}") end def has_many_attribute?(relation) @@ -76,5 +76,9 @@ def belongs_to_attribute?(relation) def reflect_association(relation) relation.klass.reflect_on_association(attribute.to_s) end + + def foreign_key(relation) + reflect_association(relation).foreign_key + end end end