From 9cc324a3f1348430e5b6db58bbddab5090c708c8 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 21 Sep 2015 11:14:32 -0600 Subject: Ensure aliased attributes passed to `select` are quoted if using `from` Fixes #21488 [Sean Griffin & johanlunds] --- activerecord/CHANGELOG.md | 6 ++++++ activerecord/lib/active_record/relation/query_methods.rb | 2 +- activerecord/test/cases/relations_test.rb | 10 ++++++++++ activerecord/test/schema/schema.rb | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1b016b6e8b..8befdfcdba 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Ensure `select` quotes aliased attributes, even when using `from`. + + Fixes #21488 + + *Sean Griffin & @johanlunds* + * MySQL: support `unsigned` numeric data types. Example: diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index e25b889851..038280509d 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -250,7 +250,7 @@ module ActiveRecord def _select!(*fields) # :nodoc: fields.flatten! fields.map! do |field| - klass.attribute_alias?(field) ? klass.attribute_alias(field) : field + klass.attribute_alias?(field) ? klass.attribute_alias(field).to_sym : field end self.select_values += fields self diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 8256762f96..5aa0ea85a5 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1897,4 +1897,14 @@ class RelationTest < ActiveRecord::TestCase def test_relation_join_method assert_equal 'Thank you for the welcome,Thank you again for the welcome', Post.first.comments.join(",") end + + def test_selecting_aliased_attribute_quotes_column_name_when_from_is_used + klass = Class.new(ActiveRecord::Base) do + self.table_name = :test_with_keyword_column_name + alias_attribute :description, :desc + end + klass.create!(description: "foo") + + assert_equal ["foo"], klass.select(:description).from(klass.all).map(&:desc) + end end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index df1834d381..d318f3a968 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -943,6 +943,10 @@ ActiveRecord::Schema.define do t.string :token t.string :auth_token end + + create_table :test_with_keyword_column_name, force: true do |t| + t.string :desc + end end Course.connection.create_table :courses, force: true do |t| -- cgit v1.2.3