What is the difference between build and new on Rails?

前端 未结 5 1570
無奈伤痛
無奈伤痛 2020-12-02 09:11

Can anyone tell me what is the difference between build and new command on Rails?

5条回答
  •  无人及你
    2020-12-02 10:10

    Prior to rails 2.2, you needed build for the has_many/has_and_belongs_to_many part of the relation in order for the new record to automatically have it's foreign keys set. For example:

    user.timesheets.build
    

    would set the foreign key. I think for rails 2.2 and onwards, new and build do the same thing for has_many and has_and_belongs_to_many relations.

提交回复
热议问题