[Rails] What is the difference between new and build?

225 views
Skip to first unread message

Manoj Kumar

unread,
Apr 23, 2010, 3:38:02 AM4/23/10
to rubyonra...@googlegroups.com
What is the difference between

parent.children << child.new

and

parent.children.build(params[:child])
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Sharagoz

unread,
Apr 23, 2010, 4:40:02 AM4/23/10
to Ruby on Rails: Talk
In the first one, the child is saved right away.
In the second, the child is not saved until the parent is saved

You may find this usefull:
http://blog.mrbrdo.net/2009/10/27/ruby-on-rails-new-vs-create-vs-build/

On Apr 23, 9:38 am, Manoj Kumar <li...@ruby-forum.com> wrote:
> What is the difference between
>
> parent.children << child.new
>
> and
>
> parent.children.build(params[:child])
> --
> Posted viahttp://www.ruby-forum.com/.

Manoj Kumar

unread,
Apr 26, 2010, 12:18:04 AM4/26/10
to rubyonra...@googlegroups.com
Sharagoz wrote:
> In the first one, the child is saved right away.
> In the second, the child is not saved until the parent is saved
>
> You may find this usefull:
> http://blog.mrbrdo.net/2009/10/27/ruby-on-rails-new-vs-create-vs-build/

Thanks Sharagoz. I got some idea about this.
--
Posted via http://www.ruby-forum.com/.

Francesco Mari

unread,
Feb 13, 2018, 4:57:36 AM2/13/18
to Ruby on Rails: Talk
Actually if the parent is not persisted, the shovel operator does not save data on db.
Reply all
Reply to author
Forward
0 new messages