简体   繁体   English

RAILS:具有现有记录的新方法中的嵌套属性

[英]RAILS: Nested attributes in new method with existing record

I have models: 我有模特:

Frame.rb 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. 问题是Frame.new是一个新记录,当ActiveRecord到达参数manufacturers_attributes它对Frame.new的关联manufacturers_attributes执行查找,这是未保存的,因此没有用于执行查找的id。

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). 我建议从现有的manufacturer记录开始,然后简单地创建像manufacturer.frames.create(frame_params)这样的框架(假设一对多的关系)。

However, if you must do it this way you can overwrite the manufacturer_attributes method like so: 但是,如果必须这样做,则可以覆盖manufacturer_attributes方法,如下所示:

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. 因此,您在原始manufacturer_attributes尝试在新记录上访问它之前分配制造商,该记录先前导致了错误。

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. 新框架现在具有指定的制造商,因此当它尝试使用manufacturer_attributes更新制造商时,它可以正确找到它。

If you only want to assign the existing Manufacturer and not update any attributes then you don't need the manufacturer_attributes . 如果您只想分配现有的制造商而不更新任何属性,那么您不需要manufacturer_attributes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveRecord(Rails 2.3.8)-更新现有属性,更新嵌套属性时添加新记录 - ActiveRecord (Rails 2.3.8) - Update existing, add new record when updating nested attributes Rails:使用嵌套属性中的信息更新现有记录 - Rails: Updating existing record with information from nested attributes accepts_nested_attributes_for链接到现有记录,而不是创建新记录 - accepts_nested_attributes_for to link to existing record, not create a new one 带有现有记录的 Rails 嵌套属性 - Rails nested attributes with existing records Rails 在添加新的嵌套记录时错误解释 http 方法 - Rails wrongly interpreting http method when adding new nested record 具有嵌套属性和has_many:through关系的表单创建新记录,而不使用现有记录 - Form with nested attributes and has_many :through relationship create new record instead of using existing one 使用accepts_nested_attributes_for将现有has_many记录添加到新记录 - Adding existing has_many records to new record with accepts_nested_attributes_for Rails-在Mongoid /活动记录中禁用嵌套属性 - Rails - Disable Nested Attributes in Mongoid / Active Record Rails嵌套属性与连接模型,其中一个加入的模型是新记录 - Rails nested attributes with a join model, where one of the models being joined is a new record Rails 4的嵌套属性未在当前项目中添加记录,而是在添加新项目记录 - Rails 4 nested attributes not adding record to current project, instead it's adding new project records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM