문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top