Skip to content

Commit

Permalink
Merge pull request #7536 from pivotal/fix_pluck_with_reserved_words
Browse files Browse the repository at this point in the history
Fix pluck when columns/tables are reserved words.
  • Loading branch information
rafaelfranca committed Sep 5, 2012
2 parents fdcbc56 + c7d752f commit ebc4d8c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* Fix `ActiveRecord::Relation#pluck` when columns or tables are reserved words.

*Ian Lesperance*

* Fix time column type casting for invalid time string values to correctly return nil.

*Adam Meehan*
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def calculate(operation, column_name, options = {})
def pluck(*column_names)
column_names.map! do |column_name|
if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
"#{table_name}.#{column_name}"
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
else
column_name
end
Expand Down
11 changes: 9 additions & 2 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require "cases/helper"
require 'models/club'
require 'models/company'
require "models/contract"
require 'models/topic'
require 'models/edge'
require 'models/club'
require 'models/organization'
require 'models/possession'
require 'models/topic'

Company.has_many :accounts

Expand Down Expand Up @@ -576,4 +577,10 @@ def test_pluck_with_multiple_columns_and_includes
assert_equal ["37signals", nil], companies_and_developers.first
assert_equal ["test", 7], companies_and_developers.last
end

def test_pluck_with_reserved_words
Possession.create!(:where => "Over There")

assert_equal ["Over There"], Possession.pluck(:where)
end
end
3 changes: 3 additions & 0 deletions activerecord/test/models/possession.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Possession < ActiveRecord::Base
self.table_name = 'having'
end
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ def create_table(*args, &block)
t.string :info
end

create_table :having, :force => true do |t|
t.string :where
end

create_table :guids, :force => true do |t|
t.column :key, :string
end
Expand Down

0 comments on commit ebc4d8c

Please sign in to comment.