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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Mar-26 11:51 UTC
Re: has_many with :dependent => :destroy not destroying
On Mar 26, 10:29 am, Chris Kottom <ch...-kMviOf/NVQxZWXO/OqhO/A@public.gmane.org> wrote:> 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 >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> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Chris Kottom
2011-Mar-26 12:42 UTC
Re: Re: has_many with :dependent => :destroy not destroying
And it works as advertised. Thanks a lot, Fred. On Sat, Mar 26, 2011 at 12:51 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Mar 26, 10:29 am, Chris Kottom <ch...-kMviOf/NVQxZWXO/OqhO/A@public.gmane.org> wrote: > > 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 > > > 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 > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.