Accepts Nested Attribute with a virtual attribute
For Rails 5.1 and up it's advisable to use attribute
instead of attr_accessor
as it dirties up the object, thus triggering the validation.
class Task < ActiveRecord::Base
attribute :name, :string
end
I ran into the same problem. Using :autosave => true
didn't even work for me. I managed to solve it by adding attribute_will_change!(:my_virtual_attribute)
to the writer for my virtual attribute. So, in your case:
class Task < ActiveRecord::Base
..
def name=(the_name)
attribute_will_change!(:name)
..
end
..
end
This marks the object as unchanged or dirty, and that makes update_attributes save the nested model correctly.
Links:
http://apidock.com/rails/ActiveRecord/Dirty/attribute_will_change%21 http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects