Skip to content

Commit

Permalink
Close rails#5990 Against 3.2 stable
Browse files Browse the repository at this point in the history
Conflicts:
	activerecord/lib/active_record/relation/calculations.rb
	activerecord/test/cases/calculations_test.rb
  • Loading branch information
twinturbo authored and parndt committed Nov 25, 2012
1 parent 83e7105 commit a08069a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 12 additions & 5 deletions activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.