Ruby-on-rails new + save 和 create 之间的 rails 差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9791386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differences in rails between new + save and create
提问by Matteo Pagliazzi
I'm new to rails and I don't understand the differences between the use of new+save methods and the create method.
我是 Rails 的新手,我不明白使用 new+save 方法和 create 方法之间的区别。
def create
@item = Item.new(params[:item])
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render json: @item, status: :created, location: @item }
else
format.html { render action: "new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
and:
和:
def create
respond_to do |format|
if Item.create(params[:item])
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render json: @item, status: :created, location: @item }
else
format.html { render action: "new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
采纳答案by pjumble
回答by nmott
Although it is correct that createcalls newand then savethere is a big difference between the two alternatives in their return values.
尽管create调用new然后save在返回值中两个替代方案之间存在很大差异是正确的。
Savereturns either trueor falsedepending on whether the object was saved successfully to the database or not. This can then be used for flow control as per the first example in the question above.
Save返回true或false取决于对象是否成功保存到数据库。然后可以根据上述问题中的第一个示例将其用于流量控制。
Createwill return the model regardless of whether the object was saved or not. This has implications for the code above in that the top branch of the ifstatement will always be executed even if the object fails validations and is not saved.
Create无论对象是否已保存,都将返回模型。这对上面的代码有影响,因为if即使对象未通过验证并且没有保存,语句的顶部分支也将始终执行。
If you use createwith branching logic you are at risk of silent failures which is not the case if you use new+ save.
如果您使用create分支逻辑,您将面临静默失败的风险,而如果您使用new+则情况并非如此save。
The createalternative can be useful in controllers where respond_withis used for API (JSON/XML) responses. In this case the existence of errors on the object will cause the errors to be returned in the response with a status of unprocessable_entity, which is exactly what you want from an API.
在create其中替代可以在控制器有用respond_with用于API(JSON / XML)应答。在这种情况下,对象上存在错误将导致错误在响应中返回,状态为unprocessable_entity,这正是您希望从 API 中获得的。
I would always use the new+ saveoption for html, especially if you are relying on the return value for flow control.
对于 html,我总是使用new+save选项,尤其是当您依赖返回值进行流控制时。
回答by Michael Durrant
newcreates the object but doesn't save it.
new创建对象但不保存它。
createcreates the object andsaves it, i.e. .newand .save
create创建该对象,并保存它,即.new与.save
create!creates the object and tries to save it but raises an exception if validations fails, e.g. .newand .save!
create!创建对象并尝试保存它,但如果验证失败则引发异常,例如.new和.save!
One of confusion items is that the above is the actions that you take on an object, but similar names are also given to controller methods, especially in a RESTful environment. For example you have a create action.... which creates a new object, and then saves it and another create action which just does an object create.
混淆项之一是以上是您对对象执行的操作,但控制器方法也有类似的名称,尤其是在 RESTful 环境中。例如,您有一个创建操作.... 它创建一个新对象,然后保存它,另一个创建操作只是创建一个对象。
If you're wondering "why create an object if I'm not going to save it?" consider this - the system 'tries' to save the object - but a validation prevents it and the user is asked to fill in more information on a form, perhaps required fields. One wants the object to still be created (.new) while this is going on and it will hold the values that have been assigned so far. However it doesn't actually get saved until it passes the validations as well.
如果您想知道“如果我不打算保存对象,为什么要创建它?” 考虑这一点 - 系统“尝试”保存对象 - 但验证阻止了它,并且要求用户在表单上填写更多信息,可能是必填字段。人们希望在此过程中仍会创建对象 ( .new),并且它将保存迄今为止已分配的值。然而,在save它通过验证之前它实际上并没有得到d 。
回答by Said Kaldybaev
when you use, rails actually is creating the records but didn't save it, so in the process you can also assign smth
当你使用时,rails实际上是在创建记录但没有保存它,所以在这个过程中你也可以分配smth
@item = Item.new(params[:item])
but when you use:
但是当你使用:
if Item.create(params[:item])
.....
it will immediately create and save
它会立即创建并保存
you can check it with rails c
你可以检查它 rails c

