Permalink
Browse files

Dynamic finders on association collections respect association :limit…

…. Closes #10227 [Jack Danger Canty]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8178 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 41fb490 commit 440f2890af5462402d1a77daaf1751a66742b974 @jeremy jeremy committed Nov 21, 2007
View
2 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]
View
6 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -133,7 +133,11 @@ def construct_sql
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
View
2 activerecord/lib/active_record/associations/has_many_association.rb
@@ -167,7 +167,7 @@ def construct_sql
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
View
3 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -236,7 +236,8 @@ def construct_scope
: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
View
30 activerecord/test/associations_test.rb
@@ -483,6 +483,16 @@ def test_dynamic_find_all_order_should_override_association_order
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 @@ def test_dynamic_find_all_order_should_override_association_order_for_through
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 @@ def test_dynamic_find_all_order_should_override_association_order
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')
View
1 activerecord/test/fixtures/author.rb
@@ -16,6 +16,7 @@ def testing_proxy_target
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

0 comments on commit 440f289

Please sign in to comment.