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

Fix QueryMethods#in_order_of for non-integer columns in MySQL #45623

Merged
merged 1 commit into from
Jul 19, 2022

Conversation

fatkodima
Copy link
Member

@fatkodima fatkodima commented Jul 18, 2022

CustomerOrder.in_order_of(:order_status, ['Completed', 'Cancelled', 'In Transit', 'Pending'])
=> (Object doesn't support #inspect)

Debugging the problem further revealed that it was throwing an error while constructing the SQL.

#<Arel::Visitors::UnsupportedVisitError: Unsupported argument type: String. Construct an Arel node instead.> rescued during inspection

The problem was discovered in this blog post.

@yahonda yahonda merged commit 9596ec9 into rails:main Jul 19, 2022
yahonda added a commit to yahonda/rails that referenced this pull request Jul 19, 2022
…trings

Fix `QueryMethods#in_order_of` for non-integer columns in MySQL
@yahonda
Copy link
Member

yahonda commented Jul 19, 2022

Backported to 7-0-stable branch via c99a9aa

@r7kamura
Copy link
Contributor

I wasn't aware of this because it wasn't in CHANGELOG, but I see this change is coming in Rails 7.0.4.

By the way, as a workaround in Rails 7.0.3.1, I had code that quoted on my application side, and I have confirmed that there is no problem with double quoting in Rails 7.0.4 with MySQ. So it's easy to migration from Rails 7.0.3.1 to 7.0.4 about that.
Nice fix 👍

$ bin/rails c -e test
Loading test environment (Rails 7.0.4)
irb(main):001:0> User.in_order_of(:name, %w[a b])
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`name` IN ('a', 'b') ORDER BY FIELD(`users`.`name`, 'b', 'a') DESC
=> []
irb(main):002:0> User.in_order_of(:name, %w[a b].map {|value| Arel::Nodes.build_quoted(value) })
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`name` IN ('a', 'b') ORDER BY FIELD(`users`.`name`, 'b', 'a') DESC
=> []

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

Successfully merging this pull request may close these issues.

None yet

3 participants