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

Ticket #2265 (closed defect: duplicate)

Opened 3 years ago

Last modified 1 year ago

ActiveRecord::Associations::HasOneAssociation ignores @options[:dependent] on reassignment

Reported by: ObieFernandez <obiefernandez@gmail.com> Assigned to: David
Priority: high Milestone:
Component: ActiveRecord Version: 0.13.1
Severity: normal Keywords: ActiveRecord, has_one, dependent
Cc: ObieFernandez

Description

According to the API docs, :dependent - if set to true, the associated object is destroyed when this object is. It’s also destroyed if another association is assigned.

I added the following two test cases around test_dependence in associations_test.rb (around line 136) and the second case fails. The first dependent object is not destroyed.

  def test_destroying_removes_dependent_from_db
    tw = Firm.create("name" => "ThoughtWorks")
    acct = tw.create_account("credit_limit" => 1000)
    assert_nothing_raised { Account.find(acct.id) }
    tw.destroy
    assert_raises(ActiveRecord::RecordNotFound) {
      Account.find(acct.id)
    }
  end

  def test_reassignment_removes_old_dependent_from_db
    tw = Firm.create("name" => "ThoughtWorks")
    first_dependent = tw.create_account("credit_limit" => 1000)
    second_dependent = tw.create_account("credit_limit" => 2000)
    tw.save!
    assert_raises(ActiveRecord::RecordNotFound) {
      Account.find(first_dependent.id)
    }
  end

This is similar to the dependent association bug #2226 but the fix is completely different. I will try to work on a patch soon.

Change History

01/16/07 19:33:50 changed by aeden

I've opened the ticket #7096 which fixes this ticket.

05/22/07 17:38:09 changed by kevinclark

  • status changed from new to closed.
  • resolution set to duplicate.

Closing for #7096