Skip to content
This repository

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

Merged
merged 1 commit into from over 1 year ago

3 participants

Pivotal Labs Common Effort Role Account Rafael Mendonça França Ian Lesperance
Pivotal Labs Common Effort Role Account

No description provided.

Rafael Mendonça França

Cool! Thanks

Rafael Mendonça França rafaelfranca merged commit ebc4d8c into from September 05, 2012
Rafael Mendonça França rafaelfranca closed this September 05, 2012
Yves Senn senny referenced this pull request from a commit in senny/rails September 05, 2012
Ian Lesperance 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

Showing 1 unique commit by 1 author.

Sep 05, 2012
Ian Lesperance Fix pluck when columns/tables are reserved words. c7d752f
This page is out of date. Refresh to see the latest.
4  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,9 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Fix `ActiveRecord::Relation#pluck` when columns or tables are reserved words.
  4
+
  5
+    *Ian Lesperance*
  6
+
3 7
 *   Fix time column type casting for invalid time string values to correctly return nil.
4 8
 
5 9
     *Adam Meehan*
2  activerecord/lib/active_record/relation/calculations.rb
@@ -156,7 +156,7 @@ def calculate(operation, column_name, options = {})
156 156
     def pluck(*column_names)
157 157
       column_names.map! do |column_name|
158 158
         if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
159  
-          "#{table_name}.#{column_name}"
  159
+          "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
160 160
         else
161 161
           column_name
162 162
         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
 
@@ -576,4 +577,10 @@ def test_pluck_with_multiple_columns_and_includes
576 577
     assert_equal ["37signals", nil], companies_and_developers.first
577 578
     assert_equal ["test", 7], companies_and_developers.last
578 579
   end
  580
+
  581
+  def test_pluck_with_reserved_words
  582
+    Possession.create!(:where => "Over There")
  583
+
  584
+    assert_equal ["Over There"], Possession.pluck(:where)
  585
+  end
579 586
 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
@@ -285,6 +285,10 @@ def create_table(*args, &block)
285 285
     t.string :info
286 286
   end
287 287
 
  288
+  create_table :having, :force => true do |t|
  289
+    t.string :where
  290
+  end
  291
+
288 292
   create_table :guids, :force => true do |t|
289 293
     t.column :key, :string
290 294
   end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.