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?
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
Thanks for the reply, Fred, but you left me hanging at a few spots
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)?
Correct
Do 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
hand
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
Also, what’s the difference between has_many methods delete_all and
destroy_all, size, count, and length?
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)?
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 B.
Feel free to add me to MSN and/or GTalk as this email.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.