RAILS: Nested attributes in new method with existing record

后端 未结 2 1763
野性不改
野性不改 2021-01-05 07:00

I have models:

Frame.rb

belongs_to :manufacturer, foreign_key: \'model\'
accepts_nested_attributes_for :manufacturer, :reject_if =&g         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 08:05

    If you want a new Frame with an existing manufacturer you need to assign it in the params as well as using nested attributes.

    Frame.new({name: 'Name', manufacturer_ids: [2], manufacturer_attributes: {id:2}})
    

    The new Frame now has the assigned Manufacturer so when it attempts to update the Manufacturer with the manufacturer_attributes it can find it correctly.

    If you only want to assign the existing Manufacturer and not update any attributes then you don't need the manufacturer_attributes.

提交回复
热议问题