# What's the advantage of using build and \<\<?

**URL:** <https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302>\
**Category:** Rails\
**Created:** [December 24, 2008, 5:32am UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302 "2008-12-24T05:32:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 24, 2008, 5:32am UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302/1 "2008-12-24T05:32:41Z")

</div>

Rails give us two ways to create a asociation object:  
@article = Article.new(params[:article])  
@article.comments \<\< Comment.new(params[:comment])  
@article.save!  
or

@article = Article.new(params[:article])  
@comment = @article.comments.build(params[:comment])  
@article.save!  
@comment.save!

what’s the advantage of using build and \<\< ??

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 24, 2008, 7:35am UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302/2 "2008-12-24T07:35:32Z")

</div>

## \<\< should save @article when you add a new comment.

Ryan B.  
Freelancer

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

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 24, 2008, 10:59am UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302/3 "2008-12-24T10:59:47Z")

</div>

\<\< adds records to a association-collection (no matter what kind of  
association). it returns itself (the collection) so method calls may  
be chained.

btw: in this example @article doesn’t have to be saved when a new  
comment is added (comment has article\_id as foreign key). 😉

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 24, 2008, 11:02am UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302/4 "2008-12-24T11:02:09Z")

</div>

just saw that this could be misunderstood:  
article has to be saved to be able to make a association, but it  
doesn’t have to be saved after adding a new comment.

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 24, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/whats-the-advantage-of-using-build-and/156302/5 "2008-12-24T15:29:38Z")

</div>

MaD wrote:

> \<\< adds records to a association-collection (no matter what kind of  
> association). it returns itself (the collection) so method calls may  
> be chained.
> 
> btw: in this example @article doesn’t have to be saved when a new  
> comment is added (comment has article\_id as foreign key). 😉

what’s does that -method calls may be chained-? can you make me a  
concrete example?

in this example @article doesn’t have to be saved when a new

> comment is added (comment has article\_id as foreign key)  
> =  
> @article = Article.new(params[:article])  
> don’t we have to save it for store a Article instance?
