简体   繁体   中英

Updating Rails 4 Nested Attributes

I'm having a hard time with Rails and nested attributes and would really appreciate some help.

Here is the output from my console session where I was attempting to get the updated values to save but as you can see, they don't seem to take on the next line when I perform the find again:

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">]>

The same thing is happening with the web interface. I can provide additional details if anyone needs them but I am allowing the parameters through and on creation, the initial values are saved.

I've been beating my head against this all morning and I suspect it is something stupid but I'm just not sure what to try next. Thanks!

UPDATE 1: Equipment Model:

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

And also the equipment_properties model:

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

end

Also, of relevance might be that I can update the individual equipment_property without nesting and that does work.

UPDATE 2:

I managed to add this to the controller and it saves the values now. Not pretty but it works I guess...

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

This is what I ended up adding to the controller to resolve this. Definitely a hack though and I'm not sure why the updates are taking:

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

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