在 ruby on rails 上构建方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19761766/
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
build method on ruby on rails
提问by Finks
New to rails and I'm following the Depot project found in the Agile web development with rails 3.1. Everything was fine until I got lost when the book used the "build" method.
Rails 的新手,我正在使用 Rails 3.1 关注敏捷 Web 开发中的 Depot 项目。一切都很好,直到我在本书使用“构建”方法时迷路了。
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
My google searches led me to understand that the .build method is just a cleaner way to create a row in the table (with association between tables). But on the code above, I was expecting the code would look like something like this:
我的谷歌搜索让我明白 .build 方法只是在表中创建行(表之间关联)的一种更简洁的方法。但是在上面的代码中,我希望代码看起来像这样:
@line_item = @cart.line_items.build(product_id => params[:product_id])
I don't understand why the author had to store the whole row of products( product = Product.find(params[:product_id])) instead of just getting the product_id...
我不明白为什么作者必须存储整行产品( product = Product.find(params[:product_id])) 而不是只获取 product_id ...
Is there more to it than what I can understand?
还有比我能理解的更多吗?
回答by Billy Chan
You misunderstood build. It's just an alias of new, nothing special. https://github.com/rails/rails/blob/959fb8ea651fa6638aaa7caced20d921ca2ea5c1/activerecord/lib/active_record/relation.rb#L84
你误会了build。它只是 的别名new,没什么特别的。https://github.com/rails/rails/blob/959fb8ea651fa6638aaa7caced20d921ca2ea5c1/activerecord/lib/active_record/relation.rb#L84
buildwon't "create" a record in database, just create a new object in memory so that the view can take this object and display something, especially for a form.
build不会在数据库中“创建”一条记录,只是在内存中创建一个新对象,以便视图可以使用该对象并显示某些内容,尤其是对于表单。
For your second question, yes, your way of composing by id will work as well. But a better approach is not to trust param. Instead, verify it by finding in db at first.
对于您的第二个问题,是的,您按 id 编写的方式也可以。但更好的方法是不要相信 param。相反,首先通过在 db 中查找来验证它。
回答by nzifnab
I'm going to go ahead and say you are entirely correct. Either method works and will do the same thing, but your version using just :product_idis more efficient and requires one less database query. That said, it might make sense if you need that productvariable later in the code or that specific line item calls product.{something}later so it doesn't have to fetch it by id at that point.
我要继续说你是完全正确的。任何一种方法都有效并且会做同样的事情,但是您使用的版本:product_id更有效,并且需要更少的数据库查询。也就是说,如果您product稍后需要在代码中使用该变量或product.{something}稍后调用该特定行项目,则它可能是有意义的,因此它不必在此时通过 id 获取它。
However, I personally would prefer to just set the :product_id, I see no reason to find the object first.
但是,我个人更愿意只设置:product_id,我认为没有理由先找到对象。

![Ruby-on-rails rails 比较 params[:id] 和 session[:user_id] 的值不起作用](/res/img/loading.gif)