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

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)

Patch against stable branch

  • activerecord/test/associations_test.rb

    old new  
    13941394      AND developer_id = #{developer.id} 
    13951395    end_sql 
    13961396  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 
    13971418end 
  • activerecord/lib/active_record/associations.rb

    old new  
    271271      # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by setting their foreign keys to NULL.   
    272272      #   This will also destroy the objects if they're declared as belongs_to and dependent on this model. 
    273273      # * <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. 
    274275      # * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+ 
    275276      # * <tt>collection.clear</tt> - removes every object from the collection. This destroys the associated objects if they 
    276277      #   are <tt>:dependent</tt>, deletes them directly from the database if they are <tt>:exclusively_dependent</tt>, 
     
    290291      # * <tt>Firm#clients<<</tt> 
    291292      # * <tt>Firm#clients.delete</tt> 
    292293      # * <tt>Firm#clients=</tt> 
     294      # * <tt>Firm#client_ids</tt> 
    293295      # * <tt>Firm#client_ids=</tt> 
    294296      # * <tt>Firm#clients.clear</tt> 
    295297      # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>) 
     
    583585      # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by removing their associations from the join table.   
    584586      #   This does not destroy the objects. 
    585587      # * <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. 
    586589      # * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+ 
    587590      # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects. 
    588591      # * <tt>collection.empty?</tt> - returns true if there are no associated objects. 
     
    596599      # * <tt>Developer#projects.push_with_attributes</tt> 
    597600      # * <tt>Developer#projects.delete</tt> 
    598601      # * <tt>Developer#projects=</tt> 
     602      # * <tt>Developer#project_ids</tt> 
    599603      # * <tt>Developer#project_ids=</tt> 
    600604      # * <tt>Developer#projects.clear</tt> 
    601605      # * <tt>Developer#projects.empty?</tt> 
     
    771775            association 
    772776          end 
    773777 
     778          define_method("#{Inflector.singularize(association_name)}_ids") do 
     779            send("#{association_name}").map {|entry| entry.id} 
     780          end 
     781 
    774782          define_method("#{Inflector.singularize(association_name)}_ids=") do |new_value| 
    775783            send("#{association_name}=", association_class_name.constantize.find(new_value)) 
    776784          end