Skip to content
This repository
Browse code

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
Source Rendered
... ... @@ -1,5 +1,11 @@
1 1 ## Rails 3.2.12 (unreleased) ##
2 2
  3 +* Fix `ActiveRecord::Relation#pluck` when columns or tables are reserved words.
  4 + Backport #7536.
  5 + Fix #8968.
  6 +
  7 + *Ian Lesperance + Yves Senn + Kaspar Schiess*
  8 +
3 9 * Don't run explain on slow queries for database adapters that don't support it.
4 10 Backport #6197.
5 11
2  activerecord/lib/active_record/relation/calculations.rb
@@ -178,7 +178,7 @@ def calculate(operation, column_name, options = {})
178 178 #
179 179 def pluck(column_name)
180 180 if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
181   - column_name = "#{table_name}.#{column_name}"
  181 + column_name = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
182 182 else
183 183 column_name = column_name.to_s
184 184 end
11 activerecord/test/cases/calculations_test.rb
... ... @@ -1,10 +1,11 @@
1 1 require "cases/helper"
  2 +require 'models/club'
2 3 require 'models/company'
3 4 require "models/contract"
4   -require 'models/topic'
5 5 require 'models/edge'
6   -require 'models/club'
7 6 require 'models/organization'
  7 +require 'models/possession'
  8 +require 'models/topic'
8 9
9 10 Company.has_many :accounts
10 11
@@ -503,4 +504,10 @@ def test_pluck_not_auto_table_name_prefix_if_column_joined
503 504 Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
504 505 assert_equal [7], Company.joins(:contracts).pluck(:developer_id).map(&:to_i)
505 506 end
  507 +
  508 + def test_pluck_with_reserved_words
  509 + Possession.create!(:where => "Over There")
  510 +
  511 + assert_equal ["Over There"], Possession.pluck(:where)
  512 + end
506 513 end
3  activerecord/test/models/possession.rb
... ... @@ -0,0 +1,3 @@
  1 +class Possession < ActiveRecord::Base
  2 + self.table_name = 'having'
  3 +end
4 activerecord/test/schema/schema.rb
@@ -281,6 +281,10 @@ def create_table(*args, &block)
281 281 t.string :info
282 282 end
283 283
  284 + create_table :having, :force => true do |t|
  285 + t.string :where
  286 + end
  287 +
284 288 create_table :guids, :force => true do |t|
285 289 t.column :key, :string
286 290 end

0 comments on commit 1337e92

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