Skip to content
This repository

AR::Relation#pluck: improve to work with joins #4942

Merged
merged 1 commit into from about 2 years ago

6 participants

Bogdan Gusiev Alexey Vasiliev Matt Huggins Carlos Antonio da Silva Jun Lin José Valim
Bogdan Gusiev

When Relation has some joined table, columns like id will become ambiguous.
This patch automatically plucks column from main table if possible, like:

User.joins(:projects).pluck(:id) # select users.id from users join projects on ...
José Valim josevalim merged commit e7627d2 into from February 08, 2012
José Valim josevalim closed this February 08, 2012
Bogdan Gusiev

One question: what about moving this into AR::Relation#select?
So that:

User.joins(:projects).select(:id) # select users.id from users join projects on
Alexey Vasiliev

I cannot find this commit in the branch 3.2.5, only in master. In which version of rails, this method will be fixed?

Matt Huggins

This seems like something that should be backported to 3.2, but I'm only seeing it in master branch. Any chance we can get it included in 3.2.x?

Jun Lin

It does not include in 3.2.10 nor 3.2.11, but 3.2.12(Unreleased)

https://github.com/rails/rails/blob/3-2-stable/activerecord/CHANGELOG.md

Carlos Antonio da Silva

Yup, we have to wait for the next 3.2.x release, or bundle from github, 3-2-stable branch.

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.

Feb 08, 2012
Bogdan Gusiev AR::Relation#pluck: improve to work with joins a379cb2
This page is out of date. Refresh to see the latest.
3  activerecord/lib/active_record/relation/calculations.rb
@@ -177,6 +177,9 @@ def calculate(operation, column_name, options = {})
177 177
     #   Person.where(:confirmed => true).limit(5).pluck(:id)
178 178
     #
179 179
     def pluck(column_name)
  180
+      if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
  181
+        column_name = "#{table_name}.#{column_name}"
  182
+      end
180 183
       klass.connection.select_all(select(column_name).arel).map! do |attributes|
181 184
         klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
182 185
       end
11  activerecord/test/cases/calculations_test.rb
@@ -478,4 +478,15 @@ def test_pluck_with_serialization
478 478
   def test_pluck_with_qualified_column_name
479 479
     assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
480 480
   end
  481
+
  482
+  def test_pluck_auto_table_name_prefix
  483
+    c = Company.create!(:name => "test", :contracts => [Contract.new])
  484
+    assert_equal [c.id], Company.joins(:contracts).pluck(:id)
  485
+  end
  486
+
  487
+  def test_pluck_not_auto_table_name_prefix_if_column_joined
  488
+    c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
  489
+    # No chance for typecast here
  490
+    assert_equal ["7"], Company.joins(:contracts).pluck(:developer_id)
  491
+  end
481 492
 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.