Ruby-on-rails .build、.create 和 .create 之间的区别!什么时候应该使用它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/403671/
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
The differences between .build, .create, and .create! and when should they be used?
提问by Tim Knight
So I've been seeing people using .build, .create, and .create! within their controllers more and more lately. What's the difference from just using .new and passig the param'd object and then .save? Are there pros and cons? Does using these other methods offer benefits?
所以我一直在看到人们使用 .build、.create 和 .create!最近越来越多地在他们的控制器中。与仅使用 .new 和 passig param'd 对象然后使用 .save 有什么区别?有优点和缺点吗?使用这些其他方法是否有好处?
回答by zenazn
There are a couple differences, but they're not big:
有一些差异,但它们并不大:
.createis equivalent to.newfollowed by.save. It's just more succinct..create!is equivalent to.newfollowed by.save!(throws an error if saving fails). It's also just a wee bit shorter- I think
.buildis mostlyan alias for.new. It works one way in Rails 3and another way in Rails < 3.x
.create相当于.new后面跟着.save。它只是更简洁。.create!相当于.new后跟.save!(如果保存失败则抛出错误)。它也只是短了一点- 我认为
.build是主要的别名.new。它在 Rails 3 中以一种方式工作,在 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.
然而,最重要的部分是可以通过关联(has_many等)调用这些方法以自动链接两个模型。
回答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。
create!doesn't suffer from the same issue as it raises and exception if the record is invalid.
create!如果记录无效,则不会遇到与引发和异常相同的问题。
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 rkj
#create is shorter version of new and save. #create! is throwing exception if validation was not positive.
#create 是 new 和 save 的较短版本。#创建!如果验证不是肯定的,则抛出异常。
回答by Vineeth Pradhan
I'd second the above answers. Plus for create, one cannot pass falseas an argument which you can do with save. Passing falseas an argument will skip all rails validations
我会支持上述答案。另外create,不能false作为参数传递,而您可以使用save. false作为参数传递将跳过所有 Rails 验证

