Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 25, 2012
twinturbo Close #5990 Against 3.2 stable
Conflicts:
	activerecord/lib/active_record/relation/calculations.rb
	activerecord/test/cases/calculations_test.rb
a08069a
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

No commit comments for this range

Something went wrong with that request. Please try again.