composed_of and validations don't work due to frozen object

composed_of freezes objects but if my object requires validations ie

class MyObject

validate do |record|

if fail?

record.errors.add :base, “FAIL!”

end

end

end

class Foo < ActiveRecord::Base

composed_of :my_object,

:allow_nil => true,

:mapping => [

%w[bgmd_name name],

%w[bgmd_serial_number serial_number]

]

validates_associated :my_object

end

this borks with a object is frozen error deep in lib/active_model/validations.rb

I’ve got a work around

module MySugr

This module is mainly a hack to get around a bug with composed_of freezing objects

and ActiveRecord::Validations requiring unfrozen objects

module Composable

def self.included(base)

base.send :extend, ClassMethods

base.send :include, ActiveRecord::Validations

base.send :include, InstanceMethods

base.send :define_method, :“validation_context=” do |*args|

end

end

module InstanceMethods

def initialize(*args)

errors

end

def new_record?

true

end

end

module ClassMethods

end

end

end

which I can mix into my class but it is not ideal. How to report a rails bug? The lighthouse

tracker seems invite only these days?

Brad

And I’m using Rails 3.1 RC1!

composed_of freezes objects but if my object requires validations ie

class MyObject validate do |record| if fail? record.errors.add :base, "FAIL!" end end end

class Foo < ActiveRecord::Base

composed_of :my_object, :allow_nil => true, :mapping => [ %w[bgmd_name name], %w[bgmd_serial_number serial_number] ]

validates_associated :my_object

end

this borks with a object is frozen error deep in lib/active_model/validations.rb

I've got a work around

module MySugr # This module is mainly a hack to get around a bug with composed_of freezing objects # and ActiveRecord::Validations requiring unfrozen objects module Composable

def self\.included\(base\)
  base\.send :extend, ClassMethods
  base\.send :include, ActiveRecord::Validations
  base\.send :include, InstanceMethods

  base\.send :define\_method, :&quot;validation\_context=&quot; do |\*args|
  end
end

module InstanceMethods
  def initialize\(\*args\)
    errors
  end

  def new\_record?
    true
  end

end

module ClassMethods

end

end end

which I can mix into my class but it is not ideal. How to report a rails bug? The lighthouse tracker seems invite only

Rails moved to using github issues recently.

I believe validates_associated with was intended to be used with associations rather than aggregations. You might find validates_with useful

Fred

validates_with is not really the solution. Aggregations are semantically the same as has_one relations and should be able to validate

the same as.

Looks like somebody closed the issue on github after importing from lighthouse without solving the issue

https://github.com/rails/rails/issues/727

or explaining why it will not be solved.

Not sure I agree.

All the imported issues were closed automatically at some point - it wasn't a reflection on the merit of those issues

Fred

I guess I’ll open it up again then :slight_smile:

I opened a github ticket on this issue

https://github.com/rails/rails/issues/1513