Skip to content
Browse files

Make dynamic finders honor additional passed in :conditions. Closes #…

…3569.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3463 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 8d4d88a commit 297618bddc59cdc4aaca76711874b774f9838afe Marcel Molina committed Jan 22, 2006
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Make dynamic finders honor additional passed in :conditions. #3569 [Oleg Pudeyev <pudeyo@rpi.edu>, Marcel Molina Jr.]
+
* Show a meaningful error when the DB2 adapter cannot be loaded due to missing dependencies. [Nicholas Seckar]
* Make .count work for has_many associations with multi line finder sql [schoenm@earthlink.net]
View
11 activerecord/lib/active_record/base.rb
@@ -990,8 +990,15 @@ def method_missing(method_id, *arguments)
conditions = construct_conditions_from_arguments(attribute_names, arguments)
- if arguments[attribute_names.length].is_a?(Hash)
- find(finder, { :conditions => conditions }.update(arguments[attribute_names.length]))
+ if (extra_options = arguments[attribute_names.size]).is_a?(Hash)
+ finder_options = extra_options.merge(:conditions => conditions)
+ if extra_options[:conditions]
+ with_scope(:find => {:conditions => extra_options[:conditions]}) do
+ find(finder, finder_options)
+ end
+ else
+ find(finder, finder_options)
+ end
else
send("find_#{finder}", conditions, *arguments[attribute_names.length..-1]) # deprecated API
end
View
15 activerecord/test/finder_test.rb
@@ -6,7 +6,7 @@
require 'fixtures/post'
class FinderTest < Test::Unit::TestCase
- fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts
+ fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :accounts
def test_find
assert_equal(topics(:first).title, Topic.find(1).title)
@@ -200,6 +200,19 @@ def test_find_by_one_attribute
assert_nil Topic.find_by_title("The First Topic!")
end
+ def test_find_by_one_attribute_with_order_option
+ assert_equal accounts(:signals37), Account.find_by_credit_limit(50)
+ assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC')
+ end
+
+ def test_find_by_one_attribute_with_conditions
+ assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
+ end
+
+ def test_find_by_one_attribute_with_several_options
+ assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3])
+ end
+
def test_find_by_one_missing_attribute
assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
end
View
5 activerecord/test/fixtures/accounts.yml
@@ -11,3 +11,8 @@ rails_core_account:
id: 3
firm_id: 6
credit_limit: 50
+
+last_account:
+ id: 4
+ firm_id: 2
+ credit_limit: 60

0 comments on commit 297618b

Please sign in to comment.
Something went wrong with that request. Please try again.