Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9043 from senny/backport_7536

Backport: Fix pluck when columns/tables are reserved words.
  • Loading branch information...
commit 1337e9284c038de8f0ea8a2de0d03efa630ea057 2 parents 1683afb + b6d4931
Carlos Antonio da Silva carlosantoniodasilva authored
6 activerecord/CHANGELOG.md
View
@@ -1,5 +1,11 @@
## Rails 3.2.12 (unreleased) ##
+* Fix `ActiveRecord::Relation#pluck` when columns or tables are reserved words.
+ Backport #7536.
+ Fix #8968.
+
+ *Ian Lesperance + Yves Senn + Kaspar Schiess*
+
* Don't run explain on slow queries for database adapters that don't support it.
Backport #6197.
2  activerecord/lib/active_record/relation/calculations.rb
View
@@ -178,7 +178,7 @@ def calculate(operation, column_name, options = {})
#
def pluck(column_name)
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
- column_name = "#{table_name}.#{column_name}"
+ column_name = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
else
column_name = column_name.to_s
end
11 activerecord/test/cases/calculations_test.rb
View
@@ -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
@@ -503,4 +504,10 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
assert_equal [7], Company.joins(:contracts).pluck(:developer_id).map(&:to_i)
end
+
+ def test_pluck_with_reserved_words
+ Possession.create!(:where => "Over There")
+
+ assert_equal ["Over There"], Possession.pluck(:where)
+ end
end
3  activerecord/test/models/possession.rb
View
@@ -0,0 +1,3 @@
+class Possession < ActiveRecord::Base
+ self.table_name = 'having'
+end
4 activerecord/test/schema/schema.rb
View
@@ -281,6 +281,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
Please sign in to comment.
Something went wrong with that request. Please try again.