Permalink
Browse files

Added find_all style to the new dynamic finders

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@308 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ac8fd7d commit 959f362ac483bb12dab7d0207d973f1ebadcb033 @dhh dhh committed Jan 2, 2005
View
@@ -10,6 +10,10 @@
<tt>Person.find_first(["user_name = ? AND password = ?", user_name, password])</tt>, you just do
<tt>Person.find_by_user_name_and_password(user_name, password)</tt>.
+ While primarily a construct for easier find_firsts, it can also be used as a construct for find_all by using calls like
+ <tt>Payment.find_all_by_amount(50)</tt> that is turned into <tt>Payment.find_all(["amount = ?", 50])</tt>. This is something not as equally useful,
+ though, as it's not possible to specify the order in which the objects are returned.
+
* Added that Base#find takes an optional options hash, including :conditions. Base#find_on_conditions deprecated in favor of #find with :conditions #407 [bitsweat]
* Added a db2 adapter that only depends on the Ruby/DB2 bindings (http://raa.ruby-lang.org/project/ruby-db2/) #386 [Maik Schmidt]
@@ -108,6 +108,10 @@ class StaleObjectError < ActiveRecordError #:nodoc:
# <tt>Person.find_first(["user_name = ? AND password = ?", user_name, password])</tt>, you just do
# <tt>Person.find_by_user_name_and_password(user_name, password)</tt>.
#
+ # While primarily a construct for easier find_firsts, it can also be used as a construct for find_all by using calls like
+ # <tt>Payment.find_all_by_amount(50)</tt> that is turned into <tt>Payment.find_all(["amount = ?", 50])</tt>. This is something not as equally useful,
+ # though, as it's not possible to specify the order in which the objects are returned.
+ #
# == Saving arrays, hashes, and other non-mappeable objects in text columns
#
# Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method +serialize+.
@@ -648,15 +652,16 @@ def undecorated_table_name(class_name = class_name_of_active_record_descendant(s
end
# Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) that are turned into
- # find_first(["user_name = ?", user_name]) and find_first(["user_name = ? AND password = ?", user_name, password]) respectively.
+ # find_first(["user_name = ?", user_name]) and find_first(["user_name = ? AND password = ?", user_name, password]) respectively. Also works
+ # for find_all, but using find_all_by_amount(50) that are turned into find_all(["amount = ?", 50]).
def method_missing(method_id, *arguments)
method_name = method_id.id2name
- if method_name =~ /find_by_([_a-z]+)/
- attributes = $1.split("_and_")
+ if method_name =~ /find_(all_by|by)_([_a-z]+)/
+ finder, attributes = ($1 == "all_by" ? :find_all : :find_first), $2.split("_and_")
attributes.each { |attr_name| super unless column_methods_hash[attr_name.intern] }
conditions = attributes.collect { |attr_name| "#{attr_name} = ? "}.join(" AND ")
- find_first([conditions, *arguments])
+ send(finder, [conditions, *arguments])
else
super
end
@@ -178,6 +178,14 @@ def test_find_by_two_attributes
assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
end
+ def test_find_all_by_one_attribute
+ topics = Topic.find_all_by_content("Have a nice day")
+ assert_equal 2, topics.size
+ assert topics.include?(@topics["first"].find)
+
+ assert_equal [], Topic.find_all_by_title("The First Topic!!")
+ end
+
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)
@@ -15,7 +15,7 @@ second:
title: The Second Topic's of the day
author_name: Mary
written_on: 2003-07-15 15:28:00
- content: Have a great day!
+ content: Have a nice day
approved: 1
replies_count: 2
parent_id: 1

0 comments on commit 959f362

Please sign in to comment.