Permalink
Browse files

only the new #select(...) query API should concatenate selects togeth…

…er, the old API using an argument hash for find(:all, :select => ...)/all(:select => ...) shouldn't. it didn't on 2.3, and otherwise calls to some_has_many_through.all(:select => 'DISTINCT x') fail, because they try to 'SELECT some_has_many_throughs.*, DISTINCT x'.
  • Loading branch information...
1 parent 87bbf48 commit 3f70fc233521fcfb966bc6e0ad0d3ee6ab785b49 @willbryant committed May 6, 2011
View
3 activerecord/lib/active_record/relation/spawn_methods.rb
@@ -117,10 +117,11 @@ def apply_finder_options(options)
finders = options.dup
finders.delete_if { |key, value| value.nil? }
- ([:joins, :select, :group, :order, :having, :limit, :offset, :from, :lock, :readonly] & finders.keys).each do |finder|
+ ([:joins, :group, :order, :having, :limit, :offset, :from, :lock, :readonly] & finders.keys).each do |finder|
relation = relation.send(finder, finders[finder])
end
+ relation = relation.except(:select).select(options[:select]) if options.has_key?(:select)
relation = relation.where(finders[:conditions]) if options.has_key?(:conditions)
relation = relation.includes(finders[:include]) if options.has_key?(:include)
relation = relation.extending(finders[:extend]) if options.has_key?(:extend)
View
7 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -386,6 +386,13 @@ def test_find_on_has_many_association_collection_with_include_and_conditions
assert_equal post_with_no_comments, posts(:authorless)
end
+ def test_find_on_has_many_association_collection_with_select
+ assert people(:michael).posts.all(:select => "DISTINCT title").size > 1
+ Post.update_all(:title => "all the same")
+ assert people(:michael).posts.all(:select => "DISTINCT title").size == 1
+ assert people(:michael).posts.all(:select => "DISTINCT title", :conditions => "title = 'none'").size == 0
+ end
+
def test_has_many_through_has_one_reflection
assert_equal [comments(:eager_sti_on_associations_vs_comment)], authors(:david).very_special_comments
end
View
4 activerecord/test/cases/method_scoping_test.rb
@@ -80,12 +80,12 @@ def test_scoped_find_select
end
end
- def test_scope_select_concatenates
+ def test_scope_select_replaced_by_find_option
Developer.send(:with_scope, :find => { :select => "name" }) do
developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
assert_equal 80000, developer.salary
assert developer.has_attribute?(:id)
- assert developer.has_attribute?(:name)
+ assert !developer.has_attribute?(:name)
assert developer.has_attribute?(:salary)
end
end
View
8 activerecord/test/cases/relation_scoping_test.rb
@@ -72,6 +72,14 @@ def test_scope_select_concatenates
end
end
+ def test_chained_select_concatenates
+ developer = Developer.select("id, name").select('id, salary').where("name = 'David'").first
+ assert_equal 80000, developer.salary
+ assert developer.has_attribute?(:id)
+ assert developer.has_attribute?(:name)
+ assert developer.has_attribute?(:salary)
+ end
+
def test_scoped_count
Developer.where("name = 'David'").scoping do
assert_equal 1, Developer.count

0 comments on commit 3f70fc2

Please sign in to comment.