Permalink
Browse files

Fixed CollectionAssociation#find to be compatible with Array#find

In order to make CollectionAssociation behave closer to Array
Add the ability to pass block to #find method just like Array#find does.
  • Loading branch information...
1 parent d7618c7 commit 132586ef8650386aaf392a7460b1f3bdb67d0b44 @bogdan bogdan committed with jonleighton Jul 5, 2011
@@ -78,10 +78,14 @@ def select(select = nil)
end
def find(*args)
- if options[:finder_sql]
- find_by_scan(*args)
+ if block_given?
+ load_target.find(*args) { |*block_args| yield(*block_args) }
else
- scoped.find(*args)
+ if options[:finder_sql]
+ find_by_scan(*args)
+ else
+ scoped.find(*args)
+ end
end
end
@@ -226,6 +226,10 @@ def test_finding
assert_equal 2, Firm.find(:first, :order => "id").clients.length
end
+ def test_finding_array_compatibility
+ assert_equal 2, Firm.order(:id).find{|f| f.id > 0}.clients.length
+ end
+
def test_find_with_blank_conditions
[[], {}, nil, ""].each do |blank|
assert_equal 2, Firm.find(:first, :order => "id").clients.find(:all, :conditions => blank).size

0 comments on commit 132586e

Please sign in to comment.