Skip to content

Commit a76c02d

Browse files
authored
Rails 6.1: Fix 'TypeError: can't quote ActiveRecord::Relation::QueryAttribute' (#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
1 parent d779517 commit a76c02d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
1515
- [#891](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/891) Add support for if_not_exists to indexes
1616
- [#892](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/892) Add support for if_exists on remove_column
17+
- [#883](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix quoting of ActiveRecord::Relation::QueryAttribute and ActiveModel::Attributes
1718

1819
#### Changed
1920

lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ def unprepare_sqlserver_statement(sql, binds)
2727
executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
2828

2929
binds.each_with_index do |bind, index|
30-
value = connection.quote(bind)
30+
31+
value = if bind.is_a?(::ActiveModel::Attribute) then
32+
connection.quote(bind.value_for_database)
33+
else
34+
connection.quote(bind)
35+
end
3136
executesql = executesql.sub("@#{index}", value)
3237
end
3338

lib/arel/visitors/sqlserver.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ def visit_Arel_Nodes_HomogeneousIn(o, collector)
7979

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

8888
collector.add_binds(attrs, &bind_block)
8989
# Monkey-patch end.
90+
else
91+
collector.add_binds(values, &bind_block)
9092
end
9193

9294
collector << ")"

0 commit comments

Comments
 (0)