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

Ticket #7096: has_one_dependent_destroy_on_reassign.diff

File has_one_dependent_destroy_on_reassign.diff, 3.0 kB (added by aeden, 2 years ago)

Patch and Unit Test

  • test/associations_test.rb

    old new  
    248248    firm.save! 
    249249    assert_nothing_raised { firm.destroy } 
    250250  end 
     251   
     252  def test_destroying_removes_dependent_from_db 
     253    tw = Firm.create("name" => "GlobalMegaCorp") 
     254    acct = tw.create_account("credit_limit" => 1000) 
     255    assert_nothing_raised { Account.find(acct.id) } 
     256    tw.destroy 
     257    assert_raises(ActiveRecord::RecordNotFound) { 
     258      Account.find(acct.id) 
     259    } 
     260  end 
    251261 
     262  def test_reassignment_removes_old_dependent_from_db 
     263    tw = Firm.create("name" => "GlobalMegaCorp") 
     264    first_dependent = tw.create_account("credit_limit" => 1000) 
     265    second_dependent = tw.create_account("credit_limit" => 2000) 
     266    tw.save! 
     267    assert_raises(ActiveRecord::RecordNotFound) { 
     268      Account.find(first_dependent.id) 
     269    } 
     270  end 
     271 
    252272  def test_succesful_build_association 
    253273    firm = Firm.new("name" => "GlobalMegaCorp") 
    254274    firm.save 
  • lib/active_record/associations/has_one_association.rb

    old new  
    66        construct_sql 
    77      end 
    88 
     9      # Create the associated model instance, persisting it to the database 
    910      def create(attrs = {}, replace_existing = true) 
    10         new_record(replace_existing) { |klass| klass.create(attrs) } 
     11        new_record(replace_existing, false) { |klass| klass.create(attrs) } 
    1112      end 
    1213 
     14      # Create the associated mode instance, persisting it to the database 
    1315      def create!(attrs = {}, replace_existing = true) 
    14         new_record(replace_existing) { |klass| klass.create!(attrs) } 
     16        new_record(replace_existing, false) { |klass| klass.create!(attrs) } 
    1517      end 
    1618 
     19      # Build the associated model instance, but do not persist it to the database 
    1720      def build(attrs = {}, replace_existing = true) 
    18         new_record(replace_existing) { |klass| klass.new(attrs) } 
     21        new_record(replace_existing, true) { |klass| klass.new(attrs) } 
    1922      end 
    2023 
    2124      def replace(obj, dont_save = false) 
     
    7578          { :create => create_scoping } 
    7679        end 
    7780 
    78         def new_record(replace_existing
     81        def new_record(replace_existing, dont_save=true
    7982          # make sure we load the target first, if we plan on replacing the existing 
    8083          # instance. Otherwise, if the target has not previously been loaded 
    8184          # elsewhere, the instance we create will get orphaned. 
     
    8386          record = @reflection.klass.with_scope(:create => construct_scope[:create]) { yield @reflection.klass } 
    8487 
    8588          if replace_existing 
    86             replace(record, true)  
     89            replace(record, dont_save)  
    8790          else 
    8891            record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? 
    8992            self.target = record