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?