klo*_*ner 58
new用于特定模型的新实例:
foo = Foo.new
Run Code Online (Sandbox Code Playgroud)
build用于在AR关联中创建新实例:
bar = foo.build_bar # (has_one or belongs_to)
Run Code Online (Sandbox Code Playgroud)
要么
bar = foo.bars.build # (has\_many, habtm or has_many :through)
Run Code Online (Sandbox Code Playgroud)
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
更新
Per @toklands的建议,构建和新的是ActiveRecord :: Relation中定义的别名:
因此,如果类Foo has_many Bars,以下具有相同的效果:
foo.bars.new <=> foo.bars.buildBar.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 | 归档时间: |
|
| 查看次数: |
17524 次 |
| 最近记录: |