0

I'm trying to pass two-levels nested attributes hash while updating model:

class Item < ActiveRecord::Base
  has_many :steps
  accepts_nested_attributes_for :steps
end

class Step < ActiveRecord::Base
  belongs_to :item
  has_many :messages, as: :discussable
  accepts_nested_attributes_for :messages
end

class Message < ActiveRecord::Base
  belongs_to :discussable, polymorphic: true
end

So when I try to pass hash like the following, it simply doesn't say parent model that any of nested models was changed (changed_for_autosave? is false because nested_records_changed_for_autosave? is false)

{steps_attributes: [{id: "1", messages_attributes: [{body: "Some message"}]}]}

But it works fine, when some of the fields of Step model changed (other words, one level nesting works fine).

What I did right now is: @item.steps.each(&:updated_at_will_change!) in the controller, which is obviously a dirty hack. Is this intended behavior or bug of Rails?

Ivan Yurov
  • 1,499
  • 9
  • 23
  • Are you sure you are whitelisting the nested attributes in your controller? If not please add the relevant controller code? – max May 25 '15 at 18:22
  • Absolutely... actually, I tried it in console with plain hash, so strong_params is not involved. – Ivan Yurov May 25 '15 at 19:31
  • I can't reproduce this `i = Item.last`, `i.assign_attributes({steps_attributes: [{id: i.steps.last.id, messages_attributes: [{body: "Some message"}]` `i.changed_for_autosave?` => true. – max May 25 '15 at 20:26
  • Weird... Maybe something else is involved. I simplified the models for that example. What version of Rails do you use? Mine is 4.2.1 – Ivan Yurov May 25 '15 at 20:32
  • Rails 4.0.3 (I guess i should update my sample app) https://github.com/maxcal/sampleapp/tree/30443812 – max May 25 '15 at 21:06
  • I'll try to experiment with your repo and write back here later. I guess that may be an issue somewhere else in my models. They are pretty complex and I already had some strange issues with them. – Ivan Yurov May 25 '15 at 21:41
  • I can confirm, that it works fine with your repo and Rails 4.2.1. Exploring farther... – Ivan Yurov May 25 '15 at 21:45

0 Answers0