Brad Phelan
2011-Jun-06 14:38 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V2Q0clNhdVR1WklK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Brad Phelan
2011-Jun-06 14:39 UTC
Re: composed_of and validations don''t work due to frozen object
And I''m using Rails 3.1 RC1! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/bVdiOFp0bHM0WmNK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jun-06 15:27 UTC
Re: composed_of and validations don''t work due to frozen object
On Jun 6, 3:38 pm, Brad Phelan <bradphe...-c/WG92ZdtxxWk0Htik3J/w@public.gmane.org> wrote:> 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 onlyRails 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> Brad-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Brad Phelan
2011-Jun-06 18:07 UTC
Re: composed_of and validations don''t work due to frozen object
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/a05BaGd6MUtGY0VK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Jun-06 18:22 UTC
Re: composed_of and validations don''t work due to frozen object
On Jun 6, 7:07 pm, Brad Phelan <bradphe...-c/WG92ZdtxxWk0Htik3J/w@public.gmane.org> wrote:> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Brad Phelan
2011-Jun-06 19:36 UTC
Re: composed_of and validations don''t work due to frozen object
I guess I''ll open it up again then :) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/cklTNU02SWplVjhK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Brad Phelan
2011-Jun-06 19:51 UTC
Re: composed_of and validations don''t work due to frozen object
I opened a github ticket on this issue https://github.com/rails/rails/issues/1513 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/VzFla1JXZXFLMVFK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.