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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
- [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
- [#892](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/892) Add support for if_exists on remove_column
- [#883](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix quoting of ActiveRecord::Relation::QueryAttribute and ActiveModel::Attributes

#### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def unprepare_sqlserver_statement(sql, binds)
executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]

binds.each_with_index do |bind, index|
value = connection.quote(bind)

value = if bind.is_a?(::ActiveModel::Attribute) then
connection.quote(bind.value_for_database)
else
connection.quote(bind)
end
executesql = executesql.sub("@#{index}", value)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ def visit_Arel_Nodes_HomogeneousIn(o, collector)

if values.empty?
collector << @connection.quote(nil)
else
elsif @connection.prepared_statements
# Monkey-patch start. Add query attribute bindings rather than just values.
column_name = o.column_name
column_type = o.attribute.relation.type_for_attribute(o.column_name)
attrs = values.map { |value| ActiveRecord::Relation::QueryAttribute.new(column_name, value, column_type) }

collector.add_binds(attrs, &bind_block)
# Monkey-patch end.
else
collector.add_binds(values, &bind_block)
end

collector << ")"
Expand Down