Changeset 6420
- Timestamp:
- 03/14/07 01:08:45 (1 year ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/base.rb (modified) (2 diffs)
- trunk/activerecord/test/finder_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r6413 r6420 1 1 *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] 2 4 3 5 * 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 189 189 # winter.new_record? # true 190 190 # 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 # 191 198 # == Saving arrays, hashes, and other non-mappable objects in text columns 192 199 # … … 1255 1262 super unless all_attributes_exists?(attribute_names) 1256 1263 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 } 1259 1271 set_readonly_option!(options) 1260 1272 trunk/activerecord/test/finder_test.rb
r6067 r6420 386 386 end 387 387 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 388 399 def test_find_or_initialize_from_one_attribute 389 400 sig38 = Company.find_or_initialize_by_name("38signals") … … 397 408 assert_equal "John", another.author_name 398 409 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? 399 418 end 400 419