I have a join table: sitedata belongs_to :tb1 belongs_to tb2 has_many :tb3 When I add a relationship in the join table I then add data to the tb3 table and that populates the FK in tb3 with the id of the join table. no problems there. when I delete the row in the join table the data stays in the tb3 table. how do I get the tb3 data to delete? Is not has_many supposed to delete the data on the other end when the primary table no longer has a reference? If I go into the DB and make the FK a constraint with a delete on cascade it works when the data in the join table is deleted.
Matt Jones
2009-Jul-23  10:09 UTC
Re: has_many :through with join table as a has_many FK issue
Your example doesn''t make much sense from here, but you''ll probably want to look into the various :dependent options to has_many. --Matt Jones On Jul 22, 10:44 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a join table: > > sitedata > belongs_to :tb1 > belongs_to tb2 > > has_many :tb3 > > When I add a relationship in the join table I then add data to the tb3 > table and that populates the FK in tb3 with the id of the join table. > no problems there. > > when I delete the row in the join table the data stays in the tb3 > table. how do I get the tb3 data to delete? > > Is not has_many supposed to delete the data on the other end when the > primary table no longer has a reference? > > If I go into the DB and make the FK a constraint with a delete on > cascade it works when the data in the join table is deleted.
you have to add :dependent => :destroy in the association you B table has_many A then the B model will looks like has_many :A , :dependent => :destroy so when you destroy an object of B it will automatically delete all the As that was associated with that B On Jul 23, 4:09 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Your example doesn''t make much sense from here, but you''ll probably > want to look into the various :dependent options to has_many. > > --Matt Jones > > On Jul 22, 10:44 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a join table: > > > sitedata > > belongs_to :tb1 > > belongs_to tb2 > > > has_many :tb3 > > > When I add a relationship in the join table I then add data to the tb3 > > table and that populates the FK in tb3 with the id of the join table. > > no problems there. > > > when I delete the row in the join table the data stays in the tb3 > > table. how do I get the tb3 data to delete? > > > Is not has_many supposed to delete the data on the other end when the > > primary table no longer has a reference? > > > If I go into the DB and make the FK a constraint with a delete on > > cascade it works when the data in the join table is deleted.
Chris Habgood
2009-Jul-23  17:58 UTC
Re: has_many :through with join table as a has_many FK issue
Here is the layout,
TBa -> TBb -> TBc
            |
          TBd
TBa has_many TBc :through => :TBb
TBb is the join table.
TBb has_many :TBd, :dependent =>  destroy.
TBd data stays there even though the data in TBb is deleted.
In my program I make the relationship dynamically and then I add data to TBd
for the relationship in TBb.
When I delete the relationship in the join table the data still remains in
TBd.
On Thu, Jul 23, 2009 at 12:52 PM, sami
<sszaman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> you have to add :dependent => :destroy in the association
>
> you B table has_many A
>
> then the B model will looks like
>
> has_many :A , :dependent => :destroy
>
> so when you destroy an object of B it will automatically delete all
> the As that was associated with that B
>
>
> On Jul 23, 4:09 pm, Matt Jones
<al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Your example doesn''t make much sense from here, but
you''ll probably
> > want to look into the various :dependent options to has_many.
> >
> > --Matt Jones
> >
> > On Jul 22, 10:44 pm, Me
<chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > > I have a join table:
> >
> > > sitedata
> > > belongs_to :tb1
> > > belongs_to tb2
> >
> > > has_many :tb3
> >
> > > When I add a relationship in the join table I then add data to
the tb3
> > > table and that populates the FK in tb3 with the id of the join
table.
> > > no problems  there.
> >
> > > when I delete the row in the join table the data stays in the tb3
> > > table.  how do I get the tb3 data to delete?
> >
> > > Is not has_many supposed to delete the data on the other end when
the
> > > primary table no longer has a reference?
> >
> > > If I go into the DB and make the FK a constraint with a delete on
> > > cascade it works when the data in the join table is deleted.
> >
>
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---