Masatoshi Nishiguchi

Active Record Relation - new vs build

These are the same as of this writing. build is an alias of new. (See: rails/activerecord/lib/active_record/relation.rb)

# GOOD: Idiomatically correct
@user = users(:michael)
@micropost = @user.microposts.build(content: "Lorem ipsum")
# BAD: Idiomatically incorrect (functionally the same as above)
@user = users(:michael)
@micropost = Micropost.new(content: "Lorem ipsum", user_id: @user.id)

References