# Another really elementary question

**URL:** <https://www.ruby-forum.com/t/another-really-elementary-question/131310>\
**Category:** Rails\
**Created:** [March 7, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310 "2008-03-07T22:02:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 7, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310/1 "2008-03-07T22:02:49Z")

</div>

Hi,

So I’m still trying to work through a tutorial on building a CMS and  
blog. I have a blog\_controller.rb with post and comments. In my show  
action, I’m trying to get it to pull the post with the given id and  
then pull the collection of comments associated with that post’s id.

def show  
@post = Post.find(params[:id])  
flash[:post\_id]=@post.id  
@pages=Page.find(:all)  
@posts=Post.find(:all)  
@comments=Comment.find(params[@Comment.post\_id==@post.id])  
end

Then I’m supposed to iterate over the comments in a partial view  
\_comment.rhtml:

\<% @post\_comments.each do |comment| %\>  
\<%= render :partial=\>“comment”, :object =\> comment %\>  
\<% end %\>

So I’m getting a NoMethod Error in the Show view. Anyone see the  
(probably) obvious problem with the code? Also, is post\_comments an  
automatic variable created from a join table operation?

Ron

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 7, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310/2 "2008-03-07T23:02:17Z")

</div>

Hi,

@post\_comments does not get auto created. But if you have your model  
relationships set you could do:  
\<% @post.comments.each do |comment| %\>  
\<%= render :partial=\>“comment”, :object =\> comment %\>  
\<% end %\>

In your Post model you’ll need

has\_many :comments

That would simplify your code to

def show  
@post = Post.find(params[:id])  
flash[:post\_id]=@post.id  
@pages=Page.find(:all)  
@posts=Post.find(:all)  
end

Henry

On Fri, Mar 7, 2008 at 1:00 PM, Ron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> flash[:post\_id]=@post.id  
> \<% end %\>
> 
> So I’m getting a NoMethod Error in the Show view. Anyone see the  
> (probably) obvious problem with the code? Also, is post\_comments an  
> automatic variable created from a join table operation?
> 
> Ron
> 
> >

–  
Henry  
[http://www.henrywagner.org/](http://www.henrywagner.org/)

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 7, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310/3 "2008-03-07T23:02:17Z")

</div>

On 7 Mar 2008, at 21:00, Ron wrote:

> flash[:post\_id]=@post.id  
> \<% end %\>  
> @post\_comments isn’t automatically created, which is probably why  
> you’re getting a no method error  
> also, i doubt  
> @comments=Comment.find(params[@Comment.post\_id==@post.id]) does what  
> you actually want it to.

Fred

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 8, 2008, 12:16am UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310/4 "2008-03-08T00:16:58Z")

</div>

Henry W. wrote:

> has\_many :comments
> 
> That would simplify your code to
> 
> def show  
> @post = Post.find(params[:id])

Also, eager load with Post.find(params[:id], :include =\> :comments

> flash[:post\_id]=@post.id  
> @pages=Page.find(:all)  
> @posts=Post.find(:all)

What role do these play?

If you want to show a particular post, then it is unlikely you would  
need to load them all (@posts) and if the pages are related to the post,  
then you can eager load them in the same find as the post:

@post = Post.find(params[:id], :include =\> [:comments, :pages])

Then the comments are (as Henry mentioned) @post.comments and the pages  
are @post.pages

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [March 8, 2008, 12:22am UTC](https://www.ruby-forum.com/t/another-really-elementary-question/131310/5 "2008-03-08T00:22:12Z")

</div>

Hi,

I’m doing similar stuff with posts, comments, users and so.  
This tutorial helped me a LOT:

> **[Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com](https://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial)**
>
> for brazilians: click here.
> I am very happy to see that my Rails 2.0 Screencast was very well received. More than 1,500 unique visitors watched it...

It also made me reimplement all I had basically but really cleaned up my  
way of approaching the work.

Cheers
