This project is archived and is in readonly mode.
validates_associated throws 'can't modify frozen object' error
Reported by Jamie | September 17th, 2010 @ 05:35 AM
Steps to reproduce:
Create a value object using composed_of like in the Rails
documentation. Use the :converter option to recreate the value
object from the params hash when attributes= is called on the
parent. Add validations to the value object. * Run those
validations by using validates_associated in the entity where you
have composed_of.
What happens:
* A 'can't modify frozen object' error is thrown.
Why it happens:
* Not really sure, but I tracked it as far as the valid? method in
ActiveRecord::Validations. It appears to break in the "ensure" part
of the that method. This was added in commit 82485068f8b64a49cbb6.
When I overrode valid? in my value object, using the logic as it
was before that commit, validates_associated started working as
expected.
Comments and changes to this ticket
-
Bob Lail December 30th, 2010 @ 04:11 PM
I dug into this a little more and wrote a blog post about the problem: Using validates_associated with composed_of and ActiveModel::Validations.
In a nutshell:
- ActiveRecord::Aggregations (deliberately) freezes objects assigned to a composed attribute
- But ActiveModel::Validations tries to create two ivars:
@errors
and@validation_context
on the frozen object
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>