更新Rails 4嵌套属性

时间:2015-11-02 17:20:01

标签: ruby-on-rails ruby-on-rails-4 nested-forms nested-attributes

我在使用Rails和嵌套属性时遇到了困难,非常感谢一些帮助。

以下是我的控制台会话的输出,我试图获取要保存的更新值,但正如您所看到的,当我再次执行查找时,它们似乎不会占用下一行:

irb(main):070:0* e = Equipment.find(26)
  Equipment Load (0.5ms)  SELECT  "equipment".* FROM "equipment" WHERE "equipment"."id" = $1 LIMIT 1  [["id", 26]]
=> #<Equipment id: 26, name: "fdsfsdsdfsd2", created_at: "2015-11-02 15:26:43", updated_at: "2015-11-02 16:38:55", site_id: 57, type_id: 3>
irb(main):071:0> e.update({"name"=>"fdsfsdsdfsd2", "site_id"=>"57", "type_id"=>"3", "equipment_properties_attributes"=>{"0"=>{"id"=>"15", "value"=>"2015-10-34", "property_id"=>"4"}, "1"=>{"id"=>"16", "value"=>"fsdfdsfsd", "property_id"=>"5"}}})
   (0.6ms)  BEGIN
  EquipmentProperty Load (0.7ms)  SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1 AND "equipment_properties"."id" IN (15, 16)  [["equipment_id", 26]]
   (0.2ms)  COMMIT
=> true
irb(main):072:0> e.equipment_properties
  EquipmentProperty Load (0.5ms)  SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1  [["equipment_id", 26]]
=> #<ActiveRecord::Associations::CollectionProxy [#<EquipmentProperty id: 15, equipment_id: 26, property_id: 4, value: "2015-10-34", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">, #<EquipmentProperty id: 16, equipment_id: 26, property_id: 5, value: "fsdfdsfsd", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">]>
irb(main):073:0> e = Equipment.find(26)
  Equipment Load (0.5ms)  SELECT  "equipment".* FROM "equipment" WHERE "equipment"."id" = $1 LIMIT 1  [["id", 26]]
=> #<Equipment id: 26, name: "fdsfsdsdfsd2", created_at: "2015-11-02 15:26:43", updated_at: "2015-11-02 16:38:55", site_id: 57, type_id: 3>
irb(main):074:0> e.equipment_properties
  EquipmentProperty Load (0.6ms)  SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1  [["equipment_id", 26]]
=> #<ActiveRecord::Associations::CollectionProxy [#<EquipmentProperty id: 15, equipment_id: 26, property_id: 4, value: "2015-10-30", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">, #<EquipmentProperty id: 16, equipment_id: 26, property_id: 5, value: "fsdfdsfsd", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">]>

Web界面也发生了同样的事情。如果有人需要,我可以提供其他详细信息,但我允许参数通过和创建时,保存初始值。

我整个上午一直在打击我的头,我怀疑这是愚蠢的,但我不确定下一步该尝试什么。谢谢!

更新1: 设备型号:

class Equipment < ActiveRecord::Base
  belongs_to :site
  belongs_to :type
  has_and_belongs_to_many :properties
  has_many :equipment_properties

  accepts_nested_attributes_for :equipment_properties, reject_if: :all_blank, allow_destroy: true
end

还有equipment_properties模型:

class EquipmentProperty < ActiveRecord::Base
  belongs_to :equipment
  belongs_to :property
  has_one :type, through: :equipment

end

另外,相关性可能是我可以在没有嵌套的情况下更新单个equipment_property,这确实有效。

更新2:

我设法将此添加到控制器并立即保存值。不是很漂亮,但它确实有效...

equipment_params[:equipment_properties_attributes].each do |property|
      ep = EquipmentProperty.where(id: property[1][:id]).first
      #logger.debug "EP Value: #{ep.value}"
      #logger.debug "Property Value: #{property[1][:value]}"
      ep.value = property[1][:value]
      ep.save
    end

1 个答案:

答案 0 :(得分:0)

这是我最终添加到控制器以解决此问题。绝对是一个黑客,但我不确定更新的原因:

equipment_params[:equipment_properties_attributes].each do |property|
      ep = EquipmentProperty.where(id: property[1][:id]).first
      #logger.debug "EP Value: #{ep.value}"
      #logger.debug "Property Value: #{property[1][:value]}"
      ep.value = property[1][:value]
      ep.save
end