ruby on rails, creating new object, use create or new method?

Michael Durrant picture Michael Durrant · Nov 27, 2011 · Viewed 19.2k times · Source

I am trying to create an object via an API, i.e. no forms are required, should I be doing MyModel.new(:name => params[:name]) or MyModel.create(:name => params[:name]) ?

Assume I have resources : my_models in routes

I checked and I see that methods can use the params hash ok.

Answer

jamiethepiper picture jamiethepiper · Nov 27, 2011

.new makes an instance (but you'll still need to .save it).
while
.create makes an instance and saves it in one go.

Hopefully that helps your decision on which to use.