有没有人能告诉我Rails中build和new命令的区别是什么?
有没有人能告诉我Rails中build和new命令的区别是什么?
new用于创建特定模型的新实例:
foo = Foo.new
build用于在AR关联中创建一个新实例:
bar = foo.build_bar # (has_one or belongs_to)
或者bar = foo.bars.build # (has\_many, habtm or has_many :through)
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
更新
根据 @toklands 的建议,在 ActiveRecord::Relation 中,build 和 new 是别名:
所以如果 Foo 类有多个 Bars,以下操作具有相同的效果:
foo.bars.new
<=> foo.bars.build
Bar.where(:foo_id=>foo.id).new
<=> Bar.where(:foo_id=>foo.id).build
如果 !foo.new_record?
:
foo.bars.new
<=> Bar.where(:foo_id=>foo.id).new
根据文档,New 和 Build 是相同的。请参见 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb
在 Rails 2.2 之前,你需要构建 has_many/has_and_belongs_to_many 关系的部分,以便新记录自动设置其外键。例如:
user.timesheets.build
将设置外键。我认为对于Rails 2.2及其以后版本,对于has_many和has_and_belongs_to_many关系,new和build的作用是相同的。
我注意到在使用嵌套资源创建“虚拟”视图表单对象时,.build和.new之间存在差异。
.build会创建一个parent_id,而.new则不会。
嵌套资源示例: @list.items(其中Item嵌套在List下)
@list.items.build ...会生成一个除了list_id之外所有值都为nil的对象。
Item.new会创建一个新的item对象,其中所有值都为nil。
当我在迭代@list.items时,在我的“show”页面中出现了它。这并不是问题,直到我需要在同一“show”页面中的另一个表单中进一步使用@list.items,其中对@list.items的迭代会暴露由.build生成的具有关联list_id但没有其他内容的项。
@list.items.build => #
2.2.3 :002 > Item.all.build => #
2.2.3 :002 > Item.all.build => #
2.2.3 :003 > Item.new => # 2.2.3 :004 >
2.2.3 :003 > 创建新项目 => # 2.2.3 :004 >