The differences between .build, .create, and .create! and when should they be used?

There are a couple differences, but they’re not big:

  1. .create is equivalent to .new followed by .save. It’s just more succinct.
  2. .create! is equivalent to .new followed by .save! (throws an error if saving fails). It’s also just a wee bit shorter
  3. I think .build is mostly an alias for .new. It works one way in Rails 3 and another way in Rails < 3.x

The most important part, however, is that these methods can be called through an association (has_many, etc.) to automatically link the two models.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)