Permalink
Browse files

handle joins/includes correctly for pluck (Fix #5990)

  • Loading branch information...
1 parent 9fd6403 commit 3e934f9aa7898f8f80023e1e7ca5a93cf2f61a3e @aderyabin aderyabin committed Apr 27, 2012
@@ -125,7 +125,12 @@ def pluck(column_name)
column_name = "#{table_name}.#{column_name}"
end
- result = klass.connection.select_all(select(column_name).arel, nil, bind_values)
+ if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
+ return construct_relation_for_association_calculations.pluck(column_name)
+ else
+ result = @klass.connection.select_all(select(column_name).arel, nil, bind_values)
+ end
+
types = result.column_types.merge klass.column_types
column = types[key]
@@ -478,6 +478,11 @@ def test_pluck_auto_table_name_prefix
assert_equal [c.id], Company.joins(:contracts).pluck(:id)
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
+
def test_pluck_not_auto_table_name_prefix_if_column_joined
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
assert_equal [7], Company.joins(:contracts).pluck(:developer_id)

0 comments on commit 3e934f9

Please sign in to comment.