Forum: Ruby on Rails How to ensure deletes cascade to associated models?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Carl Y. (Guest)
on 2005-12-15 09:01
(Received via mailing list)
I have the following models:

class Resume
  belongs_to :resume_assignments
end

class User
  belongs_to :resume_assignments
end

class ResumeAssignment
  has_one :resume
  has_one :user
end

When I delete a resume, I want to make sure that its associated
ResumeAssignment gets deleted also.  Calling destroy_all to delete
some resumes doesn't seem to do this.  Is there an option that will
make it do this?

Thanks,
Carl
Joe Van D. (Guest)
on 2005-12-15 09:22
(Received via mailing list)
Carl Y. (Guest)
on 2005-12-15 20:25
(Received via mailing list)
Thanks, but I'm looking for something slightly different.  I don't
want resumes or users to be deleted when I delete a ResumeAssignment.
I want ResumeAssignments to be deleted when I delete a resume or a
user.  The ResumeAssignment model used to be a habtm table, but I
found myself wanting to store more information in it and found that it
would be more useful as a model.  Is there a better way of setting up
the models to accomplish what I'm trying to do?
Joe Van D. (Guest)
on 2005-12-15 21:19
(Received via mailing list)
On 12/15/05, Carl Y. <removed_email_address@domain.invalid> wrote:
> Thanks, but I'm looking for something slightly different.  I don't
> want resumes or users to be deleted when I delete a ResumeAssignment.
> I want ResumeAssignments to be deleted when I delete a resume or a
> user.  The ResumeAssignment model used to be a habtm table, but I
> found myself wanting to store more information in it and found that it
> would be more useful as a model.  Is there a better way of setting up
> the models to accomplish what I'm trying to do?

Two options:

  1.  Put in an after_destroy callback in Resume and User that
destroys the ResumeAssignment

  2.  Have Resume has_one :resume_assignment and User has_one
:resume_assignment and ResumeAssignment belongs_to the others.  That's
probably your best bet.

So,
class Resume < AR; has_one :resume_assignment :dependent => true; end
class User < AR; has_one :resume_assignment :dependent => true; end
class ResumeAssignment; belongs_to :resume; belongs_to :user; end
Julian 'Julik' Tarkhanov (Guest)
on 2005-12-15 21:22
(Received via mailing list)
On 15-dec-2005, at 19:24, Carl Y. wrote:

>> http://api.rubyonrails.org/classes/ActiveRecord/As...
>>>   belongs_to :resume_assignments
>>> make it do this?
Try join models in the trunk (if you are still in development and
don't mind running edge).
Carl Y. (Guest)
on 2005-12-16 01:10
(Received via mailing list)
On 12/15/05, Joe Van D. <removed_email_address@domain.invalid> wrote:
> Two options:
>
>   1.  Put in an after_destroy callback in Resume and User that
> destroys the ResumeAssignment
>
>   2.  Have Resume has_one :resume_assignment and User has_one
> :resume_assignment and ResumeAssignment belongs_to the others.  That's
> probably your best bet.

Unfortunately only option one will work for me, since a single resume
can be assigned to multiple users and vice versa.

Thanks for the help.

Carl
Carl Y. (Guest)
on 2005-12-16 01:13
(Received via mailing list)
On 12/15/05, Carl Y. <removed_email_address@domain.invalid> wrote:
> >   2.  Have Resume has_one :resume_assignment and User has_one
> > :resume_assignment and ResumeAssignment belongs_to the others.  That's
> > probably your best bet.
>
> Unfortunately only option one will work for me, since a single resume
> can be assigned to multiple users and vice versa.

Or I guess I could make the resume has_many :resume_assignments and
the user has_many :resume_assignments and have the ResumeAssignment
belong_to the others.  That might work.

Carl
Carl Y. (Guest)
on 2005-12-16 01:35
(Received via mailing list)
By the way, this idea worked.  Thanks Joe
Joe Van D. (Guest)
on 2005-12-16 07:36
(Received via mailing list)
So the join keys are in the resume_assignments table?  That's the way
it should be, afaik.
This topic is locked and can not be replied to.