# Has\_many comments, belongs to post

**URL:** <https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262>\
**Category:** Rails\
**Created:** [April 3, 2010, 5:35am UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262 "2010-04-03T05:35:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 3, 2010, 5:35am UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/1 "2010-04-03T05:35:04Z")

</div>

I made a posts scaffold, and a comments scaffold

Now i want to link the two of them together by –

Post–

has\_many :comments

and comments-

belongs\_to :post

After that, I have no idea what to do in the controller or form for my  
comments

What do i need to change in the comments controller and form so every  
comment that gets created is assigned to a post?

Btw, i already have a post\_id:integer column in my comments table

I REALLY NEED HELP!! thanks a lot

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 3, 2010, 9:44am UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/2 "2010-04-03T09:44:21Z")

</div>

On 3 April 2010 04:33, David Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I REALLY NEED HELP!! thanks a lot

Can I suggest you work through the rails guides at  
[http://guides.rubyonrails.org/](http://guides.rubyonrails.org/). Start with Getting Started and work  
through them.

Also an excellent tutorial is being developed at

> **[Ruby on Rails Tutorial](https://www.railstutorial.org/)**
>
> Since its initial publication in 2010, the Ruby on Rails Tutorial has been one of the leading introductions to web development. In this bestselling tutorial, you’ll learn how to develop and deploy real, industrial-strength web applications with Ruby...

Colin

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 3, 2010, 2:28pm UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/3 "2010-04-03T14:28:11Z")

</div>

I think you have already set up the has\_many relationship. That is all  
it takes.  
Now you can display comments below every post by using a selection  
operation matching the post\_id colums in both tables.

something like…

find all where post\_id = post\_post  
hope you got it.

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 3, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/4 "2010-04-03T18:45:08Z")

</div>

hey guys, thanks for the replies

But those tutorials still dont tell you what to do in the contrller or  
form view for my comments

I know i need to change something in the CREATE action of my comments  
controller, so every comment that is created gets assigned to a post.  
What exactly is it?

Thanks

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 4, 2010, 9:52am UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/5 "2010-04-04T09:52:57Z")

</div>

On 3 April 2010 17:44, David Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey guys, thanks for the replies
> 
> But those tutorials still dont tell you what to do in the contrller or  
> form view for my comments

There are many tutorials out there that show how to make a basic blog  
with comments. Try google for rails tutorial blog.

Colin

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 4, 2010, 10:07am UTC](https://www.ruby-forum.com/t/has-many-comments-belongs-to-post/186262/6 "2010-04-04T10:07:01Z")

</div>

On Fri, Apr 2, 2010 at 8:33 PM, David Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I REALLY NEED HELP!! thanks a lot

David, Ryan B. has redone the classic Rails blog screencast which  
answers  
your  
your question(s) quite nicely. Thus, you can find the screencast here  
and  
following  
along:

[http://media.rubyonrails.org/video/rails\_blog\_2.mov](http://media.rubyonrails.org/video/rails_blog_2.mov)

Good luck,

-Conrad
