Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4942 from bogdan/pluck_joins

AR::Relation#pluck: improve to work with joins
  • Loading branch information...
commit e7627d2af341def8a8c5786321298db6dc7b9eb3 2 parents 17497e5 + a379cb2
@josevalim josevalim authored
View
3  activerecord/lib/active_record/relation/calculations.rb
@@ -177,6 +177,9 @@ def calculate(operation, column_name, options = {})
# Person.where(:confirmed => true).limit(5).pluck(:id)
#
def pluck(column_name)
+ if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
+ column_name = "#{table_name}.#{column_name}"
+ end
klass.connection.select_all(select(column_name).arel).map! do |attributes|
klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
end
View
11 activerecord/test/cases/calculations_test.rb
@@ -478,4 +478,15 @@ def test_pluck_with_serialization
def test_pluck_with_qualified_column_name
assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
end
+
+ def test_pluck_auto_table_name_prefix
+ c = Company.create!(:name => "test", :contracts => [Contract.new])
+ assert_equal [c.id], Company.joins(:contracts).pluck(:id)
+ end
+
+ def test_pluck_not_auto_table_name_prefix_if_column_joined
+ c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
+ # No chance for typecast here
+ assert_equal ["7"], Company.joins(:contracts).pluck(:developer_id)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.