简体   繁体   中英

RAILS: Nested attributes in new method with existing record

I have models:

Frame.rb

belongs_to :manufacturer, foreign_key: 'model'
accepts_nested_attributes_for :manufacturer, :reject_if => proc { |obj| obj.blank? }

When i try to create new Frame with existing manufacturer i get an error:

Frame.new({name: 'Name of the frame', manufacturer_attributes: {id:2}})

Error:

Couldn't find Manufacturer with ID=2 for Frame with ID=

The problem is that Frame.new is a new record, when ActiveRecord reaches the parameter manufacturers_attributes it performs a lookup on the association manufacturers_attributes for Frame.new which is unsaved and hence has no id with which to perform the lookup.

I recommend starting with the existing manufacturer record and simply create the frame like so manufacturer.frames.create(frame_params) (assuming a one-to-many relationship).

However, if you must do it this way you can overwrite the manufacturer_attributes method like so:

accepts_nested_attributes_for :manufacturer
  def manufacturer_attributes=(attributes)
    if attributes['id'].present?
      self.manufacturer = Manufacturer.find(attributes['id'])
    end
    super
  end

Thus, you assign the manufacturer before the original manufacturer_attributes tries to access it on the new record, which previously caused the error.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM