Ticket #2917: habtm-singular-id-get-method-stable.patch
| File habtm-singular-id-get-method-stable.patch, 4.0 kB (added by François Beausoleil <francois.beausoleil@gmail.com>, 4 years ago) |
|---|
-
activerecord/test/associations_test.rb
old new 1394 1394 AND developer_id = #{developer.id} 1395 1395 end_sql 1396 1396 end 1397 1398 def test_returns_collection_of_ids 1399 francois = Developer.new("name" => "Francois") 1400 francois.projects.concat([Project.find(1), Project.find(2)]) 1401 francois.save 1402 1403 assert_equal 2, francois.project_ids.size 1404 assert francois.project_ids.include?(Project.find(1).id) 1405 assert francois.project_ids.include?(Project.find(2).id) 1406 end 1407 1408 def test_assign_through_collection_of_ids 1409 francois = Developer.new("name" => "Francois") 1410 francois.project_ids = [Project.find(1).id, Project.find(2).id] 1411 francois.save 1412 francois.reload 1413 1414 assert_equal 2, francois.projects.size 1415 assert francois.projects.include?(Project.find(1)) 1416 assert francois.projects.include?(Project.find(2)) 1417 end 1397 1418 end -
activerecord/lib/active_record/associations.rb
old new 271 271 # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by setting their foreign keys to NULL. 272 272 # This will also destroy the objects if they're declared as belongs_to and dependent on this model. 273 273 # * <tt>collection=objects</tt> - replaces the collections content by deleting and adding objects as appropriate. 274 # * <tt>collection_singular_ids</tt> - returns the primary key ID of all associated objects. 274 275 # * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+ 275 276 # * <tt>collection.clear</tt> - removes every object from the collection. This destroys the associated objects if they 276 277 # are <tt>:dependent</tt>, deletes them directly from the database if they are <tt>:exclusively_dependent</tt>, … … 290 291 # * <tt>Firm#clients<<</tt> 291 292 # * <tt>Firm#clients.delete</tt> 292 293 # * <tt>Firm#clients=</tt> 294 # * <tt>Firm#client_ids</tt> 293 295 # * <tt>Firm#client_ids=</tt> 294 296 # * <tt>Firm#clients.clear</tt> 295 297 # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>) … … 583 585 # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by removing their associations from the join table. 584 586 # This does not destroy the objects. 585 587 # * <tt>collection=objects</tt> - replaces the collections content by deleting and adding objects as appropriate. 588 # * <tt>collection_singular_ids</tt> - returns an array of all primary ID of the associated objects. 586 589 # * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+ 587 590 # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects. 588 591 # * <tt>collection.empty?</tt> - returns true if there are no associated objects. … … 596 599 # * <tt>Developer#projects.push_with_attributes</tt> 597 600 # * <tt>Developer#projects.delete</tt> 598 601 # * <tt>Developer#projects=</tt> 602 # * <tt>Developer#project_ids</tt> 599 603 # * <tt>Developer#project_ids=</tt> 600 604 # * <tt>Developer#projects.clear</tt> 601 605 # * <tt>Developer#projects.empty?</tt> … … 771 775 association 772 776 end 773 777 778 define_method("#{Inflector.singularize(association_name)}_ids") do 779 send("#{association_name}").map {|entry| entry.id} 780 end 781 774 782 define_method("#{Inflector.singularize(association_name)}_ids=") do |new_value| 775 783 send("#{association_name}=", association_class_name.constantize.find(new_value)) 776 784 end