new用于特定模型的新实例:
foo = Foo.new
build用于在AR关联中创建新实例:
bar = foo.build_bar # (has_one 或 belongs_to)
或
bar = foo.bars.build # (has\_many, habtm 或 has_many :through)
http://api.rubyonrails.或g/classes/ActiveRec或d/Associations/ClassMethods.html
Update
Per @toklands's suggestion, build and new are aliases as defined in ActiveRec或d::Relation:
因此,如果Foo类有很多条,那么下面的效果是相同的:
foo.bars.new
<=> foo.bars.build
Bar.where(:foo_id=>foo.id).new
<=> Bar.where(:foo_id=>foo.id).build
And if !foo.new_rec或d?
foo.bars.new
<=> Bar.where(:foo_id=>foo.id).new