hi all, I have a table "memberships". It has a field user_id, which I want to use as a foreign key to the "users" table. So, as far as I get it, in the membership model I have to write: has_many :users In the users model, I write: belongs_to :membership However(in case the above is correct), how do I ensure that if I delete a user, the related entities in membership will be also deleted?? Thank you :) -- 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 -~----------~----~----~----~------~----~------~--~---
Pesho Petrov wrote:> hi all, > > I have a table "memberships". It has a field user_id, which I want to > use as a foreign key to the "users" table. >In that case you should have : "has_many :memberships" in the User model class and "belongs_to :user" in the Membership model class If you want all related memberships destroyed when deleting a user, you must write this in the User class: "has_many :memberships, :dependent=>:destroy" (See http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001789 for more info about :destroy option) -- 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 -~----------~----~----~----~------~----~------~--~---
On Tue, Mar 3, 2009 at 5:19 PM, Pesho Petrov <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a table "memberships". It has a field user_id, which I want to > use as a foreign key to the "users" table. > > So, as far as I get it, in the membership model I have to write: > has_many :users > > In the users model, I write: > belongs_to :membership > > However(in case the above is correct), how do I ensure that if I delete > a user, the related entities in membership will be also deleted??Check the options for "has_one" and "has_many" (e.g. on page 329 and page 331 in the Second Edition of "Agile Web Development with Rails"). Use "destroy" on the parent object (not delete). The option to the has_one or has_many you want is: :dependent => :destroy or :dependent => :delete_all depending on your design. The child row(s) will be destroyed at the time the parent is destroyed. HTH, Peter --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you, guys :-) -- 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 -~----------~----~----~----~------~----~------~--~---