Skip to content

Commit

Permalink
Don't assume the foreign key will be ${FOO}_id
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Feb 13, 2020
1 parent 7847fde commit 3453a8e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/administrate/order.rb
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 3453a8e

Please sign in to comment.