administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Use reflection table name when ordering on association columns

Open jordanstephens opened this issue 2 years ago • 0 comments

Ordering on association columns is broken if the associated model uses a non-standard table name. attribute.tableize is not sufficient.

For example, given these models

class Foo < ApplicationRecord
  has_one :bar
end

class Bar < ApplicationRecord
  self.table_name = "namespace_bars"
  belongs_to :foo
end

Administrate::Order assumes that ordering Foos by bar.name should add ORDER BY bars.name when in this case, it should be ORDER BY namespace_bars.name. The reflection contains the table_name, so this is easy to resolve by passing the reflection around to a few more.

jordanstephens avatar Oct 25 '23 22:10 jordanstephens