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 correct values when sorting by has_many associations #1800

Conversation

sinsoku
Copy link
Contributor

@sinsoku sinsoku commented Oct 15, 2020

When sorting by has_many associations, Administrate assumes that the table and column names are #{attribute}.id.
An error will occur if the user defines a different association than the table name or uses a primary key other than :id.

For example, the following code causes an error.

# db/migrate/20201016000000_create_users.rb
create_table :users, primary_key: "uid" do |t|
  t.references :company

  t.timestamps
end

# app/models/company.rb
class Company < ApplicationRecord
  has_many :employee, class_name: "User"
end

Administrate::Order.new("employee", "asc").apply(Company.all).take
#  Company Load (0.5ms)  SELECT "companies".* FROM "companies" LEFT OUTER JOIN "users" ON "users"."company_id" = "companies"."id" GROUP BY "companies"."id" ORDER BY COUNT(employee.id) asc LIMIT ?  [["LIMIT", 1]]
# Traceback (most recent call last):
#        1: from (irb):1
# ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: employee.id)

This commit fixes Administrate::Order to use the correct table and column names.

@sinsoku sinsoku force-pushed the use_correct_values_when_sorting_by_has_many_associations branch from 988ec18 to f8c0c71 Compare October 15, 2020 17:07
Copy link
Collaborator

@pablobm pablobm left a comment

Choose a reason for hiding this comment

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

Thank you for this PR! It looks good to me 👍

@sinsoku sinsoku force-pushed the use_correct_values_when_sorting_by_has_many_associations branch from f8c0c71 to b219a34 Compare October 22, 2020 13:19
When sorting by has_many associations, Administrate assumes that
the table and column names are `#{attribute}.id`.
An error will occur if the user defines a different association than
the table name or uses a primary key other than :id.

For example, the following code causes an error.

```ruby
# db/migrate/20201016000000_create_users.rb
create_table :users, primary_key: "uid" do |t|
  t.references :company

  t.timestamps
end

# app/models/company.rb
class Company < ApplicationRecord
  has_many :employee, class_name: "User"
end

Administrate::Order.new("employee", "asc").apply(Company.all).take
#   Company Load (0.6ms)  SELECT "companies".* FROM "companies" LEFT OUTER JOIN "users" ON "users"."company_id" = "companies"."id" GROUP BY "companies"."id" ORDER BY COUNT(employee.id) asc LIMIT ?  [["LIMIT", 1]]
# Traceback (most recent call last):
#         1: from (irb):14
# ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: employee.id)
```

This commit fixes `Administrate::Order` to use the correct table and column names.
@sinsoku sinsoku force-pushed the use_correct_values_when_sorting_by_has_many_associations branch from b219a34 to 4387e68 Compare October 22, 2020 13:23
@sinsoku
Copy link
Contributor Author

sinsoku commented Oct 22, 2020

I fixed conflicts and force-pushed.

@nickcharlton nickcharlton merged commit 9931774 into thoughtbot:master Oct 26, 2020
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

3 participants