# Qs with has\_many and belongs\_to

**URL:** <https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319>\
**Category:** Rails\
**Created:** [December 27, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319 "2007-12-27T17:02:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 27, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319/1 "2007-12-27T17:02:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 27, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319/2 "2007-12-27T17:20:11Z")

</div>

On 27 Dec 2007, at 16:01, Taro wrote:

> 

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

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 27, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319/3 "2007-12-27T20:17:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 28, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319/4 "2007-12-28T16:02:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 28, 2007, 1:11am UTC](https://www.ruby-forum.com/t/qs-with-has-many-and-belongs-to/124319/5 "2007-12-28T01:11:42Z")

</div>

> ## 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 B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
