diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 081030011372346cf1d99b594f8702ad70c8a1f5..b76c16792c8ce377c667e40be79b7f83d8bcb53b 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 03c0054bd060d34ba21068fb0c97f820bd940535..cb50d646586119d0a91ad4f0db4981e6b0fa948e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -188,6 +188,13 @@ def initialize(errors) # 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 @@ def method_missing(method_id, *arguments) 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 abf7f65733fa3449775a936848450ca227f06c3e..bee7c230258064ec0495d1be9d36fe5b11d4479e 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -385,6 +385,17 @@ def test_find_or_create_from_two_attributes 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 @@ def test_find_or_initialize_from_two_attributes 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" }