Categories
Technology

Rails new vs build

@article = Article.new

Creates a new instance of a Model.

@comment = @article.comments.build

Just like the new method, but by assocation proxies (not for Models). It automatically sets the foreign_key.

The other thing is that when you are doing a “has_one” the build looks like this:

@comment = @article.build_comments

I am writing this posts because searching “rails new vs build” on google did not yield any good results. I found my answer with the help of a friend, and through this post here: http://railsforum.com/viewtopic.php?pid=39621#p39621