Gotcha: Rails Doesn't Track in Place Changes to Serialized Column
Believe it or not, it’s been 6 years since the ticket was created about “Rails doesn’t track in place changes to serialized column”, it’s really a tricky problem rare people will run into. Consider the following scenario:
As commented by Jose Valim link, it’s a feature of Rails and it’s documented somewhere. The workaround is issue another options_will_change! to mark the object changed.
It gets more tricky especially when you are using accepts_nested_attributes_for, the Rails will ditch the changes to the children objects if changed? returns false.
# spec/controllers/questions_controller_spec.rbdescribeQuestionsControllerdodescribe"PUT /update"doit"updates the question with its answers"doq1=Question.createa1=Answer.create(question:q1,options:[1,2,3])put:update,id:q1.id,question:{answers_attributes:{"0"=>{id:a1.id,options:[1,2,3,4]}}}a1.reloadexpect(a1.options).toeq[1,2,3,4]# FAIL!!endendend
It really got me and hope this will help somebody like me, happy hacking!!