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?
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
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
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)?