From 6574a3b0908c9e8927dda8bd939cf46aec23c86f Mon Sep 17 00:00:00 2001 From: Filip H.F. "FiXato" Slagter Date: Thu, 30 Oct 2008 10:33:35 +0100 Subject: [PATCH] ActiveRecord::Base#with_scope now has a merge case for :select finder arguments. --- activerecord/lib/active_record/base.rb | 2 ++ activerecord/test/cases/method_scoping_test.rb | 11 +++++++++++ activerecord/test/cases/named_scope_test.rb | 7 +++++++ 3 files changed, 20 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a36a137..459c8d9 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1975,6 +1975,8 @@ module ActiveRecord #:nodoc: hash[method][key] = merge_includes(hash[method][key], params[key]).uniq elsif key == :joins && merge hash[method][key] = merge_joins(params[key], hash[method][key]) + elsif key == :select && merge + hash[method][key] = [hash[method][key],params[key]].join(',') else hash[method][key] = hash[method][key] || params[key] end diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfa..96bdd5d 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -168,6 +168,17 @@ class MethodScopingTest < ActiveRecord::TestCase assert_equal authors(:david).attributes, scoped_authors.first.attributes end + def test_scoped_find_merges_selects + author = Author.find(:first) + scoped_authors = Author.with_scope(:find => { :select => 'authors.name'}) do + scoped_authors = Author.with_scope(:find => { :select => 'authors.id'}) do + Author.find(:all, :conditions => {:id => author.id}) + end + end + assert_equal 1, scoped_authors.size + assert_equal author.attributes.delete_if{|k,v| !['name', 'id'].include?(k)}, scoped_authors.first.attributes + end + def test_scoped_count_include # with the include, will retrieve only developers for the given project Developer.with_scope(:find => { :include => :projects }) do diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 64e8997..5871f02 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -277,4 +277,11 @@ class NamedScopeTest < ActiveRecord::TestCase post = Post.find(1) assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size end + + def test_chaining_with_multiple_selects + author = Author.find(:first) + selected_attributes = author.attributes.delete_if{|k,v| !['id','name'].include?(k)} + scoped_attributes = Author.scoped(:select => 'authors.id').scoped(:select => 'authors.name', :conditions => "authors.id = #{author.id}").first.attributes + assert_equal selected_attributes, scoped_attributes + end end -- 1.5.6.4