Skip to content

Commit

Permalink
Ensure aliased attributes passed to select are quoted if using from
Browse files Browse the repository at this point in the history
Fixes #21488

[Sean Griffin & johanlunds]
  • Loading branch information
sgrif committed Sep 21, 2015
1 parent 2b85f19 commit ad07876
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Ensure `select` quotes aliased attributes, even when using `from`.

Fixes #21488

*Sean Griffin & @johanlunds*

* Correct query for PostgreSQL 8.2 compatibility.

*Ben Murphy*, *Matthew Draper*
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1809,4 +1809,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
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,10 @@ def except(adapter_names_to_exclude)
t.string :overloaded_string_with_limit, limit: 255
t.string :string_with_default, default: 'the original default'
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|
Expand Down

0 comments on commit ad07876

Please sign in to comment.