Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6420

Show
Ignore:
Timestamp:
03/14/07 01:08:45 (2 years ago)
Author:
bitsweat
Message:

find_or_create_by_* takes a hash so you can create with more attributes than are in the method name. Closes #7368.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6413 r6420  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* 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] 
  • trunk/activerecord/lib/active_record/base.rb

    r6413 r6420  
    189189  #   winter.new_record? # true 
    190190  # 
     191  # To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of 
     192  # a list of parameters. For example: 
     193  # 
     194  #   Tag.find_or_create_by_name(:name => "rails", :creator => current_user) 
     195  # 
     196  # That will either find an existing tag named "rails", or create a new one while setting the user that created it. 
     197  # 
    191198  # == Saving arrays, hashes, and other non-mappable objects in text columns 
    192199  # 
     
    12551262            super unless all_attributes_exists?(attribute_names) 
    12561263 
    1257             attributes = construct_attributes_from_arguments(attribute_names, arguments) 
    1258             options = { :conditions => attributes } 
     1264            if arguments[0].is_a?(Hash) 
     1265              attributes = arguments[0].with_indifferent_access 
     1266              find_attributes = attributes.slice(*attribute_names) 
     1267            else 
     1268              find_attributes = attributes = construct_attributes_from_arguments(attribute_names, arguments) 
     1269            end 
     1270            options = { :conditions => find_attributes } 
    12591271            set_readonly_option!(options) 
    12601272 
  • trunk/activerecord/test/finder_test.rb

    r6067 r6420  
    386386  end 
    387387   
     388  def test_find_or_create_from_one_attribute_and_hash 
     389    number_of_companies = Company.count 
     390    sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) 
     391    assert_equal number_of_companies + 1, Company.count 
     392    assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) 
     393    assert !sig38.new_record? 
     394    assert_equal "38signals", sig38.name 
     395    assert_equal 17, sig38.firm_id 
     396    assert_equal 23, sig38.client_of 
     397  end 
     398 
    388399  def test_find_or_initialize_from_one_attribute 
    389400    sig38 = Company.find_or_initialize_by_name("38signals") 
     
    397408    assert_equal "John", another.author_name 
    398409    assert another.new_record? 
     410  end 
     411   
     412  def test_find_or_initialize_from_one_attribute_and_hash 
     413    sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23}) 
     414    assert_equal "38signals", sig38.name 
     415    assert_equal 17, sig38.firm_id 
     416    assert_equal 23, sig38.client_of 
     417    assert sig38.new_record? 
    399418  end 
    400419