Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reflection table name when ordering on association columns #2450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jordanstephens
Copy link

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 jordanstephens force-pushed the order-with-reflection-table-name branch 2 times, most recently from 99fd783 to fda7fb2 Compare October 25, 2023 23:36
@jordanstephens jordanstephens marked this pull request as ready for review October 25, 2023 23:45
Copy link
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

It's not clear to me whether the test is reflecting the example you mentioned. Am I missing something, or is that not the case?

I think I'd expect to see something explicit for your example, so that we don't have regressions in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants