简体   繁体   中英

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

I am trying to create an object via an API, ie 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.

.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.

It depends on what are you want to get. new method simply instantiates new object and create method creates an object and saves it to the database, if validations pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM