Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

validates_associated throws 'can't modify frozen object' error

#5646

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.

Reported by Jamie · September 17th, 2010 @ 05:35 AM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Bob Lail
    Bob Lail

    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

    December 30th, 2010 @ 04:11 PM

  2. bingbing