diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 16 | ||||
-rw-r--r-- | activerecord/test/finder_test.rb | 19 |
3 files changed, 35 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 0810300113..b76c16792c 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* find_or_create_by_* takes a hash so you can create with more attributes than are in the method name. For example, Person.find_or_create_by_name(:name => 'Henry', :comments => 'Hi new user!') is equivalent to Person.find_by_name('Henry') || Person.create(:name => 'Henry', :comments => 'Hi new user!'). #7368 [Josh Susser] + * Make sure with_scope takes both :select and :joins into account when setting :readonly. Allows you to save records you retrieve using method_missing on a has_many :through associations. [Koz] * Allow a polymorphic :source for has_many :through associations. Closes #7143 [protocool] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 03c0054bd0..cb50d64658 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -188,6 +188,13 @@ module ActiveRecord #:nodoc: # winter = Tag.find_or_initialize_by_name("Winter") # winter.new_record? # true # + # To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of + # a list of parameters. For example: + # + # Tag.find_or_create_by_name(:name => "rails", :creator => current_user) + # + # That will either find an existing tag named "rails", or create a new one while setting the user that created it. + # # == 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+. @@ -1254,8 +1261,13 @@ module ActiveRecord #:nodoc: attribute_names = extract_attribute_names_from_match(match) super unless all_attributes_exists?(attribute_names) - attributes = construct_attributes_from_arguments(attribute_names, arguments) - options = { :conditions => attributes } + if arguments[0].is_a?(Hash) + attributes = arguments[0].with_indifferent_access + find_attributes = attributes.slice(*attribute_names) + else + find_attributes = attributes = construct_attributes_from_arguments(attribute_names, arguments) + end + options = { :conditions => find_attributes } set_readonly_option!(options) find_initial(options) || send(instantiator, attributes) diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index abf7f65733..bee7c23025 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -385,6 +385,17 @@ class FinderTest < Test::Unit::TestCase assert !another.new_record? end + def test_find_or_create_from_one_attribute_and_hash + number_of_companies = Company.count + sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) + assert_equal number_of_companies + 1, Company.count + assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) + assert !sig38.new_record? + assert_equal "38signals", sig38.name + assert_equal 17, sig38.firm_id + assert_equal 23, sig38.client_of + end + def test_find_or_initialize_from_one_attribute sig38 = Company.find_or_initialize_by_name("38signals") assert_equal "38signals", sig38.name @@ -397,6 +408,14 @@ class FinderTest < Test::Unit::TestCase assert_equal "John", another.author_name assert another.new_record? end + + def test_find_or_initialize_from_one_attribute_and_hash + sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) + assert_equal "38signals", sig38.name + assert_equal 17, sig38.firm_id + assert_equal 23, sig38.client_of + assert sig38.new_record? + end def test_find_with_bad_sql assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" } |