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
:dependent => true http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000472 On 12/14/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote:> 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 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Carl Youngblood
2005-Dec-15 18:24 UTC
Re: How to ensure deletes cascade to associated models?
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/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> :dependent => true > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000472 > > On 12/14/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote: > > 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?
On 12/15/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> 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> > On 12/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > :dependent => true > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000472 > > > > On 12/14/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote: > > > 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? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Julian ''Julik'' Tarkhanov
2005-Dec-15 19:21 UTC
Re: How to ensure deletes cascade to associated models?
On 15-dec-2005, at 19:24, Carl Youngblood 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? > > On 12/14/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> :dependent => true >> http://api.rubyonrails.org/classes/ActiveRecord/Associations/ >> ClassMethods.html#M000472 >> >> On 12/14/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote: >>> 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?Try join models in the trunk (if you are still in development and don''t mind running edge).
Carl Youngblood
2005-Dec-15 23:08 UTC
Re: How to ensure deletes cascade to associated models?
On 12/15/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 Youngblood
2005-Dec-15 23:11 UTC
Re: How to ensure deletes cascade to associated models?
On 12/15/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> 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 Youngblood
2005-Dec-15 23:34 UTC
Re: How to ensure deletes cascade to associated models?
By the way, this idea worked. Thanks Joe On 12/15/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote:> 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.
So the join keys are in the resume_assignments table? That''s the way it should be, afaik. On 12/15/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote:> By the way, this idea worked. Thanks Joe > > On 12/15/05, Carl Youngblood <carl-MJzSGySFh6ZUfOvSQQQpYw@public.gmane.org> wrote: > > 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. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >