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
On 12/15/05, Carl Y. [email protected] 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:
-
Put in an after_destroy callback in Resume and User that
destroys the ResumeAssignment
-
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
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).
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?
On 12/15/05, Joe Van D. [email protected] wrote:
Two options:
-
Put in an after_destroy callback in Resume and User that
destroys the ResumeAssignment
-
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
By the way, this idea worked. Thanks Joe
On 12/15/05, Carl Y. [email protected] wrote:
- 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
So the join keys are in the resume_assignments table? That’s the way
it should be, afaik.