# Controller syntax

**URL:** <https://www.ruby-forum.com/t/controller-syntax/193129>\
**Category:** Rails\
**Created:** [August 11, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/controller-syntax/193129 "2010-08-11T20:16:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Kirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nathan\_Kirk](https://www.ruby-forum.com/u/Nathan_Kirk)\
**Post date:** [August 11, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/controller-syntax/193129/1 "2010-08-11T20:16:52Z")

</div>

why is this required:

def new  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.build  
end

instead of:  
def new  
@comment = Post.find(params[:post\_id]).comments.build  
end

is the 2nd style possible? If no, why not?  
if yes, why is the 1st style preferred?

---

<div class="post-metadata">

**Author:** ![Nathan\_Kirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nathan\_Kirk](https://www.ruby-forum.com/u/Nathan_Kirk)\
**Post date:** [August 11, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/controller-syntax/193129/2 "2010-08-11T20:28:35Z")

</div>

Nathan Kirk wrote:

> why is this required:
> 
> def new  
> @post = Post.find(params[:post\_id])  
> @comment = @post.comments.build  
> end
> 
> instead of:  
> def new  
> @comment = Post.find(params[:post\_id]).comments.build  
> end
> 
> is the 2nd style possible? If no, why not?  
> if yes, why is the 1st style preferred?

Depends on your view code. Remember that controller @instance variables  
are copied to the view, so that the first example would make both @post  
and @comment available in the view, whereas the second example would  
only make @comment available.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Nathan\_Kirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nathan\_Kirk](https://www.ruby-forum.com/u/Nathan_Kirk)\
**Post date:** [August 12, 2010, 5:01am UTC](https://www.ruby-forum.com/t/controller-syntax/193129/3 "2010-08-12T05:01:24Z")

</div>

Marnen Laibow-Koser wrote:

> > def new  
> > only make @comment available.

And of course if you just had @post you could easily extract comments in  
the view code by @post.comments.build. @comment is really not necessary  
because @post gives you the handle you need.

Norm

---

<div class="post-metadata">

**Author:** ![Nathan\_Kirk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@Nathan\_Kirk](https://www.ruby-forum.com/u/Nathan_Kirk)\
**Post date:** [August 12, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/controller-syntax/193129/4 "2010-08-12T16:22:16Z")

</div>

Norm S. wrote:

> Marnen Laibow-Koser wrote:
> 
> > > def new  
> > > only make @comment available.
> 
> And of course if you just had @post you could easily extract comments in  
> the view code by @post.comments.build.

But you shouldn’t. That’s too much logic for the view.

> @comment is really not necessary  
> because @post gives you the handle you need.

Wrong. If you care about MVC, you shouldn’t be building objects in the  
view layer. (If you don’t care about MVC, you shouldn’t be using  
Rails.)

> Norm

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone
