diff options
Diffstat (limited to 'activerecord')
6 files changed, 41 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index cba28c8393..a8146ab460 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Dynamic finders on association collections respect association :order and :limit. #10211, #10227 [Patrick Joyce, Rick Olson, Jack Danger Canty] + * Add 'foxy' support for fixtures of polymorphic associations. #10183 [jbarnette, David Lowenfels] * validates_inclusion_of and validates_exclusion_of allow formatted :message strings. #8132 [devrieda, Mike Naberezny] diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 0a03efa5e4..3d10721af6 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -133,7 +133,11 @@ module ActiveRecord end def construct_scope - { :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false, :order => @reflection.options[:order] } } + { :find => { :conditions => @finder_sql, + :joins => @join_sql, + :readonly => false, + :order => @reflection.options[:order], + :limit => @reflection.options[:limit] } } end # Join tables with additional columns on top of the two foreign keys must be considered ambiguous unless a select diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 972745ee31..ead447baa0 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -167,7 +167,7 @@ module ActiveRecord def construct_scope create_scoping = {} set_belongs_to_association_for(create_scoping) - { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order] }, :create => create_scoping } + { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, :create => create_scoping } end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index a9cacb09c8..2d06e90416 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -236,7 +236,8 @@ module ActiveRecord :conditions => construct_conditions, :joins => construct_joins, :select => construct_select, - :order => @reflection.options[:order] } } + :order => @reflection.options[:order], + :limit => @reflection.options[:limit] } } end def construct_sql diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 4969d65a99..20224cbd42 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -483,6 +483,16 @@ class HasManyAssociationsTest < Test::Unit::TestCase assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id') end + def test_dynamic_find_all_should_respect_association_limit + assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length + assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length + end + + def test_dynamic_find_all_limit_should_override_association_limit + assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length + assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length + end + def test_triple_equality assert !(Array === Firm.find(:first).clients) assert Firm.find(:first).clients === Array @@ -1120,6 +1130,16 @@ class HasManyAssociationsTest < Test::Unit::TestCase assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find_all_by_type('SpecialComment', :order => 'comments.id') end + def test_dynamic_find_all_should_respect_association_limit_for_through + assert_equal 1, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'").length + assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length + end + + def test_dynamic_find_all_order_should_override_association_limit_for_through + assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length + assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length + end + end class BelongsToAssociationsTest < Test::Unit::TestCase @@ -1864,6 +1884,16 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis', :order => 'id') end + def test_dynamic_find_all_should_respect_association_limit + assert_equal 1, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'").length + assert_equal 1, projects(:active_record).limited_developers.find_all_by_name('Jamis').length + end + + def test_dynamic_find_all_order_should_override_association_limit + assert_equal 2, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'", :limit => 9_000).length + assert_equal 2, projects(:active_record).limited_developers.find_all_by_name('Jamis', :limit => 9_000).length + end + def test_new_with_values_in_collection jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis') david = DeveloperForProjectWithAfterCreateHook.find_by_name('David') diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 782a7d608f..d41d8ac439 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -16,6 +16,7 @@ class Author < ActiveRecord::Base end has_many :comments, :through => :posts has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC' + has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1 has_many :funky_comments, :through => :posts, :source => :comments has_many :special_posts |