I have this model called "User." In the user model I have many relations hips such as: has_many :creditcards has_many :airlinememberships and in my airlinemembership model I have belongs_to :user Now, what I would like to do is to delete not only the user but also the airlinmembership info asssocciated with that user when a delete action occurs. Now, I suppose I could do a find_by_id( user.id ) to go through all the tables and destroy all assocciated info, but I''m thinkin that there has to be an easier way in RoR. Plus, there are many many tables that a user may have info in which make the code not ugly but certaintly long. Also, I know about the rails framework API: http://api.rubyonrails.org/, but it seems like this site fails to really explain all the attributes that can be used in a helper method, for example. I was wondering if anyone knows of a better site that explains maybe a little better or might provide some better examples? Thanks all, ~S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Shandy Nantz wrote:> I have this model called "User." In the user model I have many relations > hips such as: > > has_many :creditcards > has_many :airlinememberships > > and in my airlinemembership model I have > > belongs_to :user > > Now, what I would like to do is to delete not only the user but also the > airlinmembership info asssocciated with that user when a delete action > occurs. Now, I suppose I could do a find_by_id( user.id ) to go through > all the tables and destroy all assocciated info, but I''m thinkin that > there has to be an easier way in RoR. Plus, there are many many tables > that a user may have info in which make the code not ugly but certaintly > long. > > Also, I know about the rails framework API: http://api.rubyonrails.org/, > but it seems like this site fails to really explain all the attributes > that can be used in a helper method, for example. I was wondering if > anyone knows of a better site that explains maybe a little better or > might provide some better examples? Thanks all, > > ~SCouldn''t you just use the foreign key and set the option cascade on delete? So that way, any record that has your client_id would be deleted. I hope this helps. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
has_many :airlinememberships, :dependent => :destroy That should do the trick. Look it up for more info. --Tyler Prete On 7/16/07, Shandy Nantz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have this model called "User." In the user model I have many relations > hips such as: > > has_many :creditcards > has_many :airlinememberships > > and in my airlinemembership model I have > > belongs_to :user > > Now, what I would like to do is to delete not only the user but also the > airlinmembership info asssocciated with that user when a delete action > occurs. Now, I suppose I could do a find_by_id( user.id ) to go through > all the tables and destroy all assocciated info, but I''m thinkin that > there has to be an easier way in RoR. Plus, there are many many tables > that a user may have info in which make the code not ugly but certaintly > long. > > Also, I know about the rails framework API: http://api.rubyonrails.org/, > but it seems like this site fails to really explain all the attributes > that can be used in a helper method, for example. I was wondering if > anyone knows of a better site that explains maybe a little better or > might provide some better examples? Thanks all, > > ~S > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I did the cascade on delete but the :dependent => :destroy is what I was looking for. Thanks you guys, either way it works perfectly. ~S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---