Skip to content
Browse files

Close #5990 Against 3.2 stable

Conflicts:
	activerecord/lib/active_record/relation/calculations.rb
	activerecord/test/cases/calculations_test.rb
  • Loading branch information...
1 parent 83e7105 commit a08069aa9cbc500a3cd3cca4818759c5db86cb63 twinturbo committed with
View
17 activerecord/lib/active_record/relation/calculations.rb
@@ -177,11 +177,18 @@ def calculate(operation, column_name, options = {})
# Person.where(:confirmed => true).limit(5).pluck(:id)
#
def pluck(column_name)
- column_name = column_name.to_s
- relation = clone
- relation.select_values = [column_name]
- klass.connection.select_all(relation.arel, nil, bind_values).map! do |attributes|
- klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
+ if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
+ column_name = "#{table_name}.#{column_name}"
+ end
+
+ if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
+ return construct_relation_for_association_calculations.pluck(column_name)
+ else
+ relation = clone
+ relation.select_values = [column_name]
+ klass.connection.select_all(relation.arel, nil, bind_values).map! do |attributes|
+ klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
+ end
end
end
View
5 activerecord/test/cases/calculations_test.rb
@@ -493,4 +493,9 @@ def test_pluck_replaces_select_clause
assert_equal [1,2,3,4], taks_relation.pluck(:id)
assert_equal [false, true, true, true], taks_relation.pluck(:approved)
end
+
+ def test_pluck_if_table_included
+ c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
+ assert_equal [c.id], Company.includes(:contracts).where("contracts.id" => c.contracts.first).pluck(:id)
+ end
end

0 comments on commit a08069a

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