Lille
2011-May-13 12:10 UTC
included module validations not seeing delegated instance methods
Hi, Maybe the form of my validations module is deficient... module MyValidationsModule extend ActiveSupport::Concern included do validate :some_custom_validation end def some_custom_validation <references to various instance methods of parent, e.g., ref_1 & ref_2, here> end end ...because when I include this module in a parent class and then in that parent delegate to the methods of an associated AR class... delegate :ref_1, :ref_2, :prefix=>false, :to=>:associated_ar ...when the validations execute in the parent, they do not find the delegated instance methods. Any thoughts? Thanks, Lille -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Kendall Gifford
2011-May-14 02:08 UTC
Re: included module validations not seeing delegated instance methods
On Friday, May 13, 2011 6:10:19 AM UTC-6, Lille wrote:> > Hi, > > Maybe the form of my validations module is deficient... > > module MyValidationsModule > extend ActiveSupport::Concern > > included do > validate :some_custom_validation > end > > def some_custom_validation > <references to various instance methods of parent, e.g., ref_1 & > ref_2, here> > end > end >Wow, getting fancy there. Just a little off topic question, any reason why you don''t want to use the ActiveModel::Validator module and the #validates_with helper to break-out/organize your custom validations? http://guides.rubyonrails.org/active_record_validations_callbacks.html#validates_with> > ...because when I include this module in a parent class and then in > that parent delegate to the methods of an associated AR class... > > delegate :ref_1, :ref_2, :prefix=>false, :to=>:associated_ar > > ...when the validations execute in the parent, they do not find the > delegated instance methods. > > Any thoughts? > >Sure, just a few questions. If you manually define your methods #ref_1 and #ref_2 (and just put logging code in their implementations) as opposed to using delegation methods, do they get called? Also, what is the exact error message or indication of failure? Can you provide any more code? Are the delegations to a class where the parent (that includes your validation module) has_many, has_one, or belongs_to the associated model? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.