aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG5
-rwxr-xr-xactiverecord/lib/active_record/base.rb10
-rw-r--r--activerecord/test/finder_test.rb4
3 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 26e2981dd3..56ec6c80be 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* Added option for passing an array to the find_all version of the dynamic finders and have it evaluated as an IN fragment. Example:
+
+ # SELECT * FROM topics WHERE title IN ('First', 'Second')
+ Topic.find_all_by_title(["First", "Second"])
+
* Added compatibility with camelCase column names for dynamic finders #533 [Dee.Zsombor]
* Fixed extraneous comma in count() function that made it not work with joins #1156 [jarkko/Dee.Zsombor]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 283eb1070a..a74f8ce471 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -787,7 +787,7 @@ module ActiveRecord #:nodoc:
attributes.each { |attr_name| super unless column_methods_hash[attr_name.intern] }
attr_index = -1
- conditions = attributes.collect { |attr_name| attr_index += 1; "#{attr_name} #{arguments[attr_index].nil? ? "IS" : "="} ? " }.join(" AND ")
+ conditions = attributes.collect { |attr_name| attr_index += 1; "#{attr_name} #{attribute_condition(arguments[attr_index])} " }.join(" AND ")
if arguments[attributes.length].is_a?(Hash)
find(finder, { :conditions => [conditions, *arguments[0...attributes.length]]}.merge(arguments[attributes.length]))
@@ -800,6 +800,14 @@ module ActiveRecord #:nodoc:
end
end
+ def attribute_condition(argument)
+ case argument
+ when nil then "IS ?"
+ when Array then "IN (?)"
+ else "= ?"
+ end
+ end
+
# Defines an "attribute" method (like #inheritance_column or
# #table_name). A new (class) method will be created with the
# given name. If a value is specified, the new method will
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 0c672c2e35..3748e26f5e 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -211,6 +211,10 @@ class FinderTest < Test::Unit::TestCase
assert @topics["first"].find, topics.first
end
+ def test_find_all_by_array_attribute
+ assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic's of the day"]).size
+ end
+
def test_find_all_by_boolean_attribute
topics = Topic.find_all_by_approved(false)
assert_equal 1, topics.size