Skip to content

Rails 6.1: Fix 'TypeError: can't quote ActiveRecord::Relation::QueryAttribute' #883

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

Conversation

mgrunberg
Copy link
Contributor

There are some test failing due to TypeError: can't quote ActiveRecord::Relation::QueryAttribute.

I found two places where this was happening:

  1. visit_Arel_Nodes_HomogeneousIn is binding ActiveRecord::Relation::QueryAttribute even for unprepared statements (.i.e Post.where(author: [1, 2]).to_sql). Unprepared statement uses Arel::Collectors::SubstituteBinds which quote the binded value (see abstract_adapter to confirm what I'm saying)
  2. explain receives instances of ::ActiveModel::Attribute (not only by visit_Arel_Nodes_HomogeneousIn hack).

@wpolicarpo
Copy link
Member

Can you please add a changelog entry for this one?

@aidanharan
Copy link
Contributor

@mgrunberg I have a cleaner fix for this issue in #885. Could you review and see what you think?

@mgrunberg
Copy link
Contributor Author

mgrunberg commented Apr 19, 2021

@mgrunberg I have a cleaner fix for this issue in #885. Could you review and see what you think?

@aidanharan I evaluated adding ActiveModel::Attribute to _quote but discarded it after a while. Here is what I thought,

Rails deprecated passing ActiveRecord objects to quote (rails/rails@87886c9). I consider passing ActiveModel::Attributes as something similar to the deprecated behavior. The fact that we call quote inside _quote is a smell of mixing a lower-level API with a higher one.

Also, allowing quoting of ActiveModel::Attributes but not typecasting seems inconsistent to me. Or something that can lead to confusion in the future.

Digging in Rails code (this time main branch) I keep finding places where they decide not to quote ActiveModel::Attributes directly (https://github.com/rails/rails/blob/main/activerecord/lib/arel/visitors/to_sql.rb#L106, https://github.com/rails/rails/blob/main/activerecord/lib/arel/visitors/to_sql.rb#L612)

What do you think?

aidanharan pushed a commit to aidanharan/activerecord-sqlserver-adapter that referenced this pull request Apr 20, 2021
@aidanharan
Copy link
Contributor

@mgrunberg Yeah I think your fix is better. I've created #896 to revert #885 so that this PR can be used instead.

wpolicarpo pushed a commit that referenced this pull request Apr 20, 2021
Co-authored-by: Aidan Haran <aharan@fusioneer.com>
@wpolicarpo
Copy link
Member

Hey, can you resolve the changelog conflict so I can merge it?

@mgrunberg
Copy link
Contributor Author

@wpolicarpo done

@wpolicarpo wpolicarpo merged commit a76c02d into rails-sqlserver:main Apr 20, 2021
@mgrunberg mgrunberg deleted the issues/yellowspot/rails-6-1/cant-quote-query-attribute branch January 3, 2022 17:33
lavika pushed a commit to lavika/activerecord-sqlserver-adapter that referenced this pull request Sep 26, 2023
…-sqlserver#896)

Co-authored-by: Aidan Haran <aharan@fusioneer.com>
lavika pushed a commit to lavika/activerecord-sqlserver-adapter that referenced this pull request Sep 26, 2023
…ttribute' (rails-sqlserver#883)

* don't patch unprepared statements

took the idea from

rails/rails@157f6a6#diff-39e2986a9d501a5ce3587a8d5944feb67c91e777f44270627850c219709d6510

and the fact that SubstituteCollector is only used for
unprepared_statements (see https://github.com/rails/rails/blob/v6.1.0/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb#L729)

* handle explain with ::ActiveRecord::Relation::QueryAttribute binds

Idea took from
rails/rails@157f6a6#diff-137c2c919e7860732301c49c60a700517455564bf463ff036756fb31c02a60e5

* add changelog entry

* apply suggestion
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.

3 participants