lib/mongoid/finders.rb in mongoid-1.1.4 vs lib/mongoid/finders.rb in mongoid-1.2.0
- old
+ new
@@ -61,11 +61,11 @@
#
# args: A +Hash+ of attributes
#
# <tt>Person.find_or_create_by(:attribute => "value")</tt>
def find_or_create_by(attrs = {})
- first(:conditions => attrs) || self.create(attrs)
+ find_or(:create, attrs)
end
# Find the first +Document+ given the conditions, or instantiates a new document
# with the conditions that were supplied
#
@@ -73,11 +73,11 @@
#
# args: A +Hash+ of attributes
#
# <tt>Person.find_or_initialize_by(:attribute => "value")</tt>
def find_or_initialize_by(attrs = {})
- first(:conditions => attrs) || self.new(attrs)
+ find_or(:new, attrs)
end
# Find the first +Document+ given the conditions.
#
# Options:
@@ -213,7 +213,12 @@
# Returns: <tt>Criteria</tt>
def where(selector = nil)
Criteria.new(self).where(selector)
end
+ protected
+ # Find the first object or create/initialize it.
+ def find_or(method, attrs = {})
+ first(:conditions => attrs) || send(method, attrs)
+ end
end
end