Skip to content
Browse files

Fix pluck when columns/tables are reserved words.

  • Loading branch information...
1 parent 1c8539d commit c7d752fe2f1ae79af648ccc1270d396355a18e94 @elliterate elliterate committed Sep 5, 2012
View
4 activerecord/CHANGELOG.md
@@ -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*
View
2 activerecord/lib/active_record/relation/calculations.rb
@@ -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
View
11 activerecord/test/cases/calculations_test.rb
@@ -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
@@ -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
View
3 activerecord/test/models/possession.rb
@@ -0,0 +1,3 @@
+class Possession < ActiveRecord::Base
+ self.table_name = 'having'
+end
View
4 activerecord/test/schema/schema.rb
@@ -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

0 comments on commit c7d752f

Please sign in to comment.
Something went wrong with that request. Please try again.