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

Ticket #3692: habtm_create_patch.diff

File habtm_create_patch.diff, 4.3 kB (added by josh@hasmanythrough.com, 4 years ago)

Let's try it WITH docs this time. (And correct typo in file name.)

  • activerecord/test/associations_test.rb

    old new  
    13541354    devel.save 
    13551355    assert !proj.new_record? 
    13561356    assert_equal devel.projects.last, proj 
     1357    assert_equal Developer.find(1).projects.last, proj  # prove join table is updated 
    13571358  end 
    13581359   
     1360  def test_build_by_new_record 
     1361    devel = Developer.new(:name => "Marcel", :salary => 75000) 
     1362    proj1 = devel.projects.build(:name => "Make bed") 
     1363    proj2 = devel.projects.build(:name => "Lie in it") 
     1364    assert_equal devel.projects.last, proj2 
     1365    assert proj2.new_record? 
     1366    devel.save 
     1367    assert !devel.new_record? 
     1368    assert !proj2.new_record? 
     1369    assert_equal devel.projects.last, proj2 
     1370    assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated 
     1371  end 
     1372   
    13591373  def test_create 
    13601374    devel = Developer.find(1) 
    13611375    proj = devel.projects.create("name" => "Projekt") 
    13621376    assert_equal devel.projects.last, proj 
    13631377    assert !proj.new_record? 
     1378    assert_equal Developer.find(1).projects.last, proj  # prove join table is updated 
    13641379  end 
    13651380   
     1381  def test_create_by_new_record 
     1382    devel = Developer.new(:name => "Marcel", :salary => 75000) 
     1383    proj1 = devel.projects.create(:name => "Make bed") 
     1384    proj2 = devel.projects.create(:name => "Lie in it") 
     1385    assert_equal devel.projects.last, proj2 
     1386    assert proj2.new_record? 
     1387    devel.save 
     1388    assert !devel.new_record? 
     1389    assert !proj2.new_record? 
     1390    assert_equal devel.projects.last, proj2 
     1391    assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated 
     1392  end 
     1393   
    13661394  def test_uniq_after_the_fact 
    13671395    developers(:jamis).projects << projects(:active_record) 
    13681396    developers(:jamis).projects << projects(:active_record) 
  • activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    old new  
    1313        record 
    1414      end 
    1515 
     16      def create(attributes = {}) 
     17        # Can't use Base.create since the foreign key may be a protected attribute. 
     18        if attributes.is_a?(Array) 
     19          attributes.collect { |attr| create(attr) } 
     20        else 
     21          record = build(attributes) 
     22          insert_record(record) unless @owner.new_record? 
     23          record 
     24        end 
     25      end 
     26 
    1627      def find_first 
    1728        load_target.first 
    1829      end 
  • activerecord/lib/active_record/associations.rb

    old new  
    763763      # * <tt>collection.size</tt> - returns the number of associated objects. 
    764764      # * <tt>collection.find(id)</tt> - finds an associated object responding to the +id+ and that 
    765765      #   meets the condition that it has to be associated with this object. 
     766      # * <tt>collection.build(attributes = {})</tt> - returns a new object of the collection type that has been instantiated 
     767      #   with +attributes+ and linked to this object through the join table but has not yet been saved. 
     768      # * <tt>collection.create(attributes = {})</tt> - returns a new object of the collection type that has been instantiated 
     769      #   with +attributes+ and linked to this object through the join table and that has already been saved (if it passed the validation). 
    766770      # 
    767771      # Example: An Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add: 
    768772      # * <tt>Developer#projects</tt> 
     
    775779      # * <tt>Developer#projects.empty?</tt> 
    776780      # * <tt>Developer#projects.size</tt> 
    777781      # * <tt>Developer#projects.find(id)</tt> 
     782      # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("project_id" => id)</tt>) 
     783      # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("project_id" => id); c.save; c</tt>) 
    778784      # The declaration may include an options hash to specialize the behavior of the association. 
    779785      #  
    780786      # Options are: