diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-12-27 03:25:29 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-27 03:25:29 +0530 |
commit | f6f416c58e805604390314e2e8e5ecf6a0a78b4f (patch) | |
tree | 480b570570eb25373c91b9d777806e61e2621b3f /activerecord/lib | |
parent | cc753eaf589b6dfae92f6fc0031c5710e616a26c (diff) | |
download | rails-f6f416c58e805604390314e2e8e5ecf6a0a78b4f.tar.gz rails-f6f416c58e805604390314e2e8e5ecf6a0a78b4f.tar.bz2 rails-f6f416c58e805604390314e2e8e5ecf6a0a78b4f.zip |
Add find_by_* and find_all_by_* finders to ActiveRecord::Relation
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index b6800b07b7..f9ca6cbbd2 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -136,6 +136,20 @@ module ActiveRecord @relation.send(method, *args, &block) elsif Array.method_defined?(method) to_a.send(method, *args, &block) + elsif match = DynamicFinderMatch.match(method) + attributes = match.attribute_names + super unless @klass.send(:all_attributes_exists?, attributes) + + if match.finder? + conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h} + result = where(conditions).send(match.finder) + + if match.bang? && result.blank? + raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}" + else + result + end + end else super end |