Ensure aliased attributes passed to select are quoted if using from

Fixes #21488

[Sean Griffin & johanlunds]
This commit is contained in:
Sean Griffin 2015-09-21 11:14:32 -06:00
parent dac7d0d046
commit 9cc324a3f1
4 changed files with 21 additions and 1 deletions

@ -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:

@ -250,7 +250,7 @@ def select(*fields)
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

@ -1897,4 +1897,14 @@ def test_merging_reorders_bind_params
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

@ -943,6 +943,10 @@ def except(adapter_names_to_exclude)
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|