How to ensure deletes cascade to associated models?


#1

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


#2

:dependent => true

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000472


#3

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


#4

On 15-dec-2005, at 19:24, Carl Y. wrote:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/

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).


#5

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?


#6

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


#7

By the way, this idea worked. Thanks Joe


#8

On 12/15/05, Carl Y. removed_email_address@domain.invalid wrote:

  1. 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


#9

So the join keys are in the resume_assignments table? That’s the way
it should be, afaik.