has_many with :dependent => :destroy not destroying

I have three models: Book, User, and BookOwnership. Book and User form an m–to-n relationship, but because I will have additional information about the ownership relationships, I cannot use has_and_belongs_to_many, so I’ve defined the relationships as follows:

class Book < ActiveRecord::Base

has_many :book_ownerships, :dependent => :destroy

has_many :owners, :through => :book_ownerships, :class_name => “User”

end

class User < ActiveRecord::Base

has_many :book_ownerships, :dependent => :destroy

has_many :books, :through => :book_ownerships

end

class BookOwnership < ActiveRecord::Base

belongs_to :book

belongs_to :owner, :class_name => “User”

end

Based on this, I expect that when I call #destroy on an instance of either Book or User it should also destroy the associated BookOwnership instances. But this doesn’t seem to be happening. Both of the tests below are currently failing as are similar attempts in console.

class BookOwnershipTest < ActiveSupport::TestCase

context “for a book and a user” do

setup do

  @book1 = Factory(:book)

  @user1 = Factory(:user)

end

context "with an ownership relationship" do

  setup { @oship1 = BookOwnership.create(:book => @book1, :owner => @user1) }

  should "destroy associated BookOwnerships when Book is destroyed" do

    @book1.destroy

    assert(@oship1.destroyed?)

  end

  should "destroy associated BookOwnerships when Owner is destroyed" do

    @user1.destroy

    assert(@oship1.destroyed?)

  end

end

end

Any advice as to what I might do differently here to get the desired behavior? Am I using this wrong, or is it a bug in Rails? (Using version 3.0.5.)

Thanks.

I have three models: Book, User, and BookOwnership. Book and User form an m--to-n relationship, but because I will have additional information about the ownership relationships, I cannot use has_and_belongs_to_many, so I've defined the relationships as follows:

class Book < ActiveRecord::Base has_many :book_ownerships, :dependent => :destroy has_many :owners, :through => :book_ownerships, :class_name => "User" end

class User < ActiveRecord::Base has_many :book_ownerships, :dependent => :destroy has_many :books, :through => :book_ownerships end

class BookOwnership < ActiveRecord::Base belongs_to :book belongs_to :owner, :class_name => "User" end

Based on this, I expect that when I call #destroy on an instance of either Book or User it should also destroy the associated BookOwnership instances. But this doesn't seem to be happening. Both of the tests below are currently failing as are similar attempts in console.

class BookOwnershipTest < ActiveSupport::TestCase context "for a book and a user" do setup do @book1 = Factory(:book) @user1 = Factory(:user) end

context &quot;with an ownership relationship&quot; do
  setup \{ @oship1 = BookOwnership\.create\(:book =&gt; @book1, :owner =&gt;

@user1) }

  should &quot;destroy associated BookOwnerships when Book is destroyed&quot; do
    @book1\.destroy
    assert\(@oship1\.destroyed?\)
  end

  should &quot;destroy associated BookOwnerships when Owner is destroyed&quot; do
    @user1\.destroy
    assert\(@oship1\.destroyed?\)
  end
end

end

destroyed? just checks the @destroyed ivar, ie it doesn't check the database to see if the object has been destroyed. Since rails doesn't have an identity map (yet, it's in the master branch), the actual in memory instance that gets destroyed may not be @oship1. I'd check whether the record is actually gone from the database

Fred

And it works as advertised. Thanks a lot, Fred.