I''m trying to make a discussion website, which involves using the has_many attributes. I wanted to make sure of a few things: I want my models linked this way: (Parent) Forum -> Board -> Message_thread -> Message (Child) This means, then, that the Board should hold the foreign key of Forums; Message_thread, the foreign key of Boards; and Messages, the foreign key of Message Thread? The "agile" book wasn''t too clear on this. Second, when adding and editing a child model (say, Message), do I have to search through Forum, Board, and Message_threads to add a Message? Or could I simply find the id to Message_thread and add a Message in that thread (or, in editing''s case, find and edit a corresponding Message)? What about deleting a Message? Do I have to search through Forum, Board, and message-thread for that as well? Or will the count/find/ size/length method update itself after I singly delete a Message? How would I delete a Forum? Do I have to delete all it''s children, and children of those? Thanks for the insight! --~--~---------~--~----~------------~-------~--~----~ 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 27 Dec 2007, at 16:01, Taro wrote:> > I''m trying to make a discussion website, which involves using the > has_many attributes. I wanted to make sure of a few things: > I want my models linked this way: > (Parent) Forum -> Board -> Message_thread -> Message (Child) > > This means, then, that the Board should hold the foreign key of > Forums; Message_thread, the foreign key of Boards; and Messages, the > foreign key of Message Thread? The "agile" book wasn''t too clear on > this. >Sounds about right.> Second, when adding and editing a child model (say, Message), do I > have to search through Forum, Board, and Message_threads to add a > Message? Or could I simply find the id to Message_thread and add a > Message in that thread (or, in editing''s case, find and edit a > corresponding Message)? >Correct> What about deleting a Message? Do I have to search through Forum, > Board, and message-thread for that as well? Or will the count/find/ > size/length method update itself after I singly delete a Message? How > would I delete a Forum? Do I have to delete all it''s children, and > children of those? >Deleteing a message is just deleting a row from the messages table - no messing around there. If you are deleting a forum, You can set your has_many to be dependant => :delete_all and so on (or I believe the the database can enforce that for you), or just whip up some stuff by hand Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply, Fred, but you left me hanging at a few spots :-P> > I''m trying to make a discussion website, which involves using the > > has_many attributes. I wanted to make sure of a few things: > > I want my models linked this way: > > (Parent) Forum -> Board -> Message_thread -> Message (Child) > > This means, then, that the Board should hold the foreign key of > > Forums; Message_thread, the foreign key of Boards; and Messages, the > > foreign key of Message Thread? The "agile" book wasn''t too clear on > > this. > Sounds about right.Oh good!> > Second, when adding and editing a child model (say, Message), do I > > have to search through Forum, Board, and Message_threads to add a > > Message? Or could I simply find the id to Message_thread and add a > > Message in that thread (or, in editing''s case, find and edit a > > corresponding Message)? > CorrectDo you mean "correct" to the first question, or the second?> > What about deleting a Message? Do I have to search through Forum, > > Board, and message-thread for that as well? Or will the count/find/ > > size/length method update itself after I singly delete a Message? How > > would I delete a Forum? Do I have to delete all it''s children, and > > children of those? > Deleteing a message is just deleting a row from the messages table - > no messing around there. If you are deleting a forum, You can set your > has_many to be dependant => :delete_all and so on (or I believe the > the database can enforce that for you), or just whip up some stuff by > handSo this means something like: --------------------- forum = Forum.find(0) forum.Board.each do |temp_board| temp_board.Message_thread.each do |temp_thread| #delete all messages temp_thread.delete_all end #delete all threads temp_board.delete_all end forum.delete_all ---------------------------- Also, what''s the difference between has_many methods delete_all and destroy_all, size, count, and length? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > So this means something like: > --------------------- > forum = Forum.find(0) > forum.Board.each do |temp_board| > temp_board.Message_thread.each do |temp_thread| > #delete all messages > temp_thread.delete_all > end > #delete all threads > temp_board.delete_all > end > forum.delete_all > ----------------------------Oh God, no please. Rails has this cool thing has_many called :dependent => :destroy. Set this on your forum, board, message_thread (which should just be called thread, imo) like this: Forum: has_many :boards, :dependent => :destroy Board: has_many :message_threads, :dependent => :destroy MessageThread: has_many :messages, :dependent => :destroy Deleting a forum will then cascade down and delete all messages firstly, then all message threads, all boards and finally the forum. It does it in this order just in case any models along The Path of Destruction have any validations stopping them from being deleted. -- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks a lot for the suggestion, Ryan. Just to clarify, I made the model name "message_thread" because thread is already a reserved object. Oh well. Now the only question I need answered is adding/editing child models. In the case I need to add/edit a message, do I have to search through forums, then boards, then message threads, and then append a new message in the message thread? Or do I just find a single message thread (assuming it''s already associated with a board and forum)? On Dec 27, 7:10 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > So this means something like: > > --------------------- > > forum = Forum.find(0) > > forum.Board.each do |temp_board| > > temp_board.Message_thread.each do |temp_thread| > > #delete all messages > > temp_thread.delete_all > > end > > #delete all threads > > temp_board.delete_all > > end > > forum.delete_all > > ---------------------------- > > Oh God, no please. > > Rails has this cool thing has_many called :dependent => :destroy. > > Set this on your forum, board, message_thread (which should just be called > thread, imo) like this: > > Forum: > > has_many :boards, :dependent => :destroy > > Board: > > has_many :message_threads, :dependent => :destroy > > MessageThread: > > has_many :messages, :dependent => :destroy > > Deleting a forum will then cascade down and delete all messages firstly, > then all message threads, all boards and finally the forum. It does it in > this order just in case any models along The Path of Destruction have any > validations stopping them from being deleted. > > -- > Ryan Bigghttp://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---