aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-xactiverecord/lib/active_record/base.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index da4ade6e36..5aeb8eb3a1 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -179,6 +179,12 @@ module ActiveRecord #:nodoc:
# # Now the 'Summer' tag does exist
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
#
+ # Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without saving it first. Example:
+ #
+ # # No 'Winter' tag exists
+ # winter = Tag.find_or_initialize_by_name("Winter")
+ # winter.new_record? # true
+ #
# == Saving arrays, hashes, and other non-mappable 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+.
@@ -1159,13 +1165,14 @@ module ActiveRecord #:nodoc:
else
send(deprecated_finder, conditions, *arguments[attribute_names.length..-1]) # deprecated API
end
- elsif match = /find_or_create_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
+ elsif match = /find_or_(initialize|create)_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
+ instantiator = determine_instantiator(match)
attribute_names = extract_attribute_names_from_match(match)
super unless all_attributes_exists?(attribute_names)
options = { :conditions => construct_conditions_from_arguments(attribute_names, arguments) }
set_readonly_option!(options)
- find_initial(options) || create(construct_attributes_from_arguments(attribute_names, arguments))
+ find_initial(options) || send(instantiator, construct_attributes_from_arguments(attribute_names, arguments))
else
super
end
@@ -1179,6 +1186,10 @@ module ActiveRecord #:nodoc:
match.captures.first == 'all_by' ? :find_all : :find_first
end
+ def determine_instantiator(match)
+ match.captures.first == 'initialize' ? :new : :create
+ end
+
def extract_attribute_names_from_match(match)
match.captures.last.split('_and_')
end