Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix pluck when columns/tables are reserved words. #7536

Merged
merged 1 commit into from

3 participants

@pivotalcommon

No description provided.

@rafaelfranca
Owner

Cool! Thanks

@rafaelfranca rafaelfranca merged commit ebc4d8c into rails:master
@senny senny referenced this pull request from a commit in senny/rails
@elliterate elliterate Fix pluck when columns/tables are reserved words.
Backport #7536 to fix #8968.

Conflicts:

	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/calculations.rb
b6d4931
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 5, 2012
  1. @elliterate
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.