# build versus new

**URL:** <https://discuss.rubyonrails.org/t/build-versus-new/31387>\
**Category:** rubyonrails-talk\
**Created:** [July 3, 2008, 5:04pm UTC](https://discuss.rubyonrails.org/t/build-versus-new/31387 "2008-07-03T17:04:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anna\_Lissa\_Cruz](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@Anna\_Lissa\_Cruz](https://discuss.rubyonrails.org/u/Anna_Lissa_Cruz)\
**Post date:** [July 3, 2008, 5:04pm UTC](https://discuss.rubyonrails.org/t/build-versus-new/31387/1 "2008-07-03T17:04:56Z")

</div>

I like the simplicity and elegance of creating a new associated object with:

@todo = @current\_user.todos.build(params[:todo])

versus

@todo = Todo.new(params[:todo]) @todo.user = @current\_user

But isn’t the first solution inherently slower since ‘todos’ makes a call to the database first and return all Todo objects for @current\_user?

Same with ‘find’ on a collection. That is, isn’t

@todo = @current\_user.todos.find(params[:id])

much slower than

@todo = Todo.find\_by\_id\_and\_user\_id(params[:id], @[current\_user.id](http://current_user.id)) ?

Thanks. I’m very interested in finding out why one method is preferred over the other.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 3, 2008, 5:42pm UTC](https://discuss.rubyonrails.org/t/build-versus-new/31387/2 "2008-07-03T17:42:07Z")

</div>

Nope, because neither of those will load the todos collection. @current\_user.todos does not load all todos: it's a proxy class. That class has a certain number of methods (eg build, find, etc...) for anything it can't handle it will load the collection and call the method on the resulting array,

Fred

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://discuss.rubyonrails.org/u/Lionel_Bouton)\
**Post date:** [July 3, 2008, 5:42pm UTC](https://discuss.rubyonrails.org/t/build-versus-new/31387/3 "2008-07-03T17:42:19Z")

</div>

AnnaLissa Cruz wrote:

> I like the simplicity and elegance of creating a new associated object with:
> 
> @todo = @current\_user.todos.build(params[:todo])
> 
> versus
> 
> @todo = Todo.new(params[:todo]) @todo.user = @current\_user
> 
> But isn't the first solution inherently slower since 'todos' makes a call to the database first and return all Todo objects for @current\_user?
> 
> Same with 'find' on a collection. That is, isn't
> 
> @todo = @current\_user.todos.find(params[:id])
> 
> much slower than
> 
> @todo = Todo.find\_by\_id\_and\_user\_id(params[:id], @current\_user.id \<[http://current](http://current)\_user.id\>) ?
> 
> Thanks. I'm very interested in finding out why one method is preferred over the other.

Look at your logs in development mode : @current\_user.todos doesn't fetch all objects but creates a Proxy object which then may or may not fetch all objects based on which method you call on it.

Lionel
