diff options
author | taryn <teast@globalpersonals.co.uk> | 2009-08-19 12:20:30 +0100 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-08-19 09:04:48 -0500 |
commit | 4dc05bc8a9824b9404cebecaba28f9f248f9995e (patch) | |
tree | 99d222d011c9564d33787dd433d18c0e52d808c2 /activeresource/lib | |
parent | 079ed8fc43b22529f456ef26599099ae1ed6337d (diff) | |
download | rails-4dc05bc8a9824b9404cebecaba28f9f248f9995e.tar.gz rails-4dc05bc8a9824b9404cebecaba28f9f248f9995e.tar.bz2 rails-4dc05bc8a9824b9404cebecaba28f9f248f9995e.zip |
Swallow ResourceNotFound error on find_every
Active Record does not explode with RecordNotFound if you go looking for a
collection of objects - it just returns nil. Thus Active Resource should
also not explode.
After all - finding no objects that match a set of conditions is not
exceptional behaviour - unlike looking for a specific object with a given id
(which you'd expect to exist).
I've also added documentation to +find+ to reflect this.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 88de8b1c66..293ba75ee0 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -585,6 +585,19 @@ module ActiveResource # # StreetAddress.find(1, :params => { :person_id => 1 }) # # => GET /people/1/street_addresses/1.xml + # + # == Failure or missing data + # A failure to find the requested object raises a ResourceNotFound + # exception if the find was called with an id. + # With any other scope, find returns nil when no data is returned. + # + # Person.find(1) + # # => raises ResourcenotFound + # + # Person.find(:all) + # Person.find(:first) + # Person.find(:last) + # # => nil def find(*arguments) scope = arguments.slice!(0) options = arguments.slice!(0) || {} @@ -638,16 +651,22 @@ module ActiveResource private # Find every resource def find_every(options) - case from = options[:from] - when Symbol - instantiate_collection(get(from, options[:params])) - when String - path = "#{from}#{query_string(options[:params])}" - instantiate_collection(connection.get(path, headers) || []) - else - prefix_options, query_options = split_options(options[:params]) - path = collection_path(prefix_options, query_options) - instantiate_collection( (connection.get(path, headers) || []), prefix_options ) + begin + case from = options[:from] + when Symbol + instantiate_collection(get(from, options[:params])) + when String + path = "#{from}#{query_string(options[:params])}" + instantiate_collection(connection.get(path, headers) || []) + else + prefix_options, query_options = split_options(options[:params]) + path = collection_path(prefix_options, query_options) + instantiate_collection( (connection.get(path, headers) || []), prefix_options ) + end + rescue ActiveResource::ResourceNotFound + # Swallowing ResourceNotFound exceptions and return nil - as per + # ActiveRecord. + nil end end |