[Rails] 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.
-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home