Hi, Despite close reading of Matz''s Ruby book and Agile Web Dev. w. Rails, I can''t figure out how to tackle the challenge of sharing validation code across models with attributes in common. In each of my models I''d like to validate the shared attributes with a custom validator that includes Rails ActiveRecord::Validations class methods and some of my own validations. My problem is 1) I don''t know how to pass attribute references to my special method and 2) (mundane) I can''t successfully call any ActiveRecord::Validations class methods from within my custom method. A bit of garbage here, I know, but any thoughts? Is this kind of problem handled by use of certain gems? Thanks, Grar -- 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.
Lasse Bunk
2010-Apr-01 13:41 UTC
Re: Newbie seeking advice on DRYing up my model validations...
Hi Grar, If you have a User model where you want to use some shared validations, you could create a module like this – in lib/my_email_validations.rb: module MyEmailValidations def self.included(base) base.validates_presence_of :email base.validates_length_of :email, :minimum => 6 base.validate :email_must_be_from_valid_provider end # example of a custom validator def email_must_be_from_valid_provider errors.add :email, "must be from a valid provider" if email !~ /@gmail.com$/ end end and in your User model, include them like this: class User < ActiveRecord::Base include MyEmailValidations end Does this solve your problem? /Lasse 2010/4/1 Grary <grary.stimon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Hi, > > Despite close reading of Matz''s Ruby book and Agile Web Dev. w. Rails, > I can''t figure out how to tackle the challenge of sharing validation > code across models with attributes in common. > > In each of my models I''d like to validate the shared attributes with a > custom validator that includes Rails ActiveRecord::Validations class > methods and some of my own validations. My problem is 1) I don''t know > how to pass attribute references to my special method and 2) (mundane) > I can''t successfully call any ActiveRecord::Validations class methods > from within my custom method. > > A bit of garbage here, I know, but any thoughts? Is this kind of > problem handled by use of certain gems? > > Thanks, > > Grar > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.
Grary
2010-Apr-01 15:05 UTC
Re: Newbie seeking advice on DRYing up my model validations...
Lasse, Yes, that was exactly what I needed. I see I need to explore the specifics of mixing in (which maybe Matz''s book underserves) -- in this case it was the self.include(base) expression that was key. Finally, where attributes refer logically to the same kind of data, but have different names, I have aliased them to allow the broadest use of my version of your validation module. Thanks so much, Grar On Apr 1, 9:41 am, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Grar, > > If you have a User model where you want to use some shared validations, you > could create a module like this – in lib/my_email_validations.rb: > > module MyEmailValidations > def self.included(base) > base.validates_presence_of :email > base.validates_length_of :email, :minimum => 6 > base.validate :email_must_be_from_valid_provider > end > > # example of a custom validator > def email_must_be_from_valid_provider > errors.add :email, "must be from a valid provider" if > email !~ /...@gmail.com$/ > end > end > > and in your User model, include them like this: > > class User < ActiveRecord::Base > include MyEmailValidations > end > > Does this solve your problem? > > /Lasse > > 2010/4/1 Grary <grary.sti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > Hi, > > > Despite close reading of Matz''s Ruby book and Agile Web Dev. w. Rails, > > I can''t figure out how to tackle the challenge of sharing validation > > code across models with attributes in common. > > > In each of my models I''d like to validate the shared attributes with a > > custom validator that includes Rails ActiveRecord::Validations class > > methods and some of my own validations. My problem is 1) I don''t know > > how to pass attribute references to my special method and 2) (mundane) > > I can''t successfully call any ActiveRecord::Validations class methods > > from within my custom method. > > > A bit of garbage here, I know, but any thoughts? Is this kind of > > problem handled by use of certain gems? > > > Thanks, > > > Grar > > > -- > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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.
Lasse Bunk
2010-Apr-01 15:09 UTC
Re: Re: Newbie seeking advice on DRYing up my model validations...
Nice – glad you could use it :) /Lasse 2010/4/1 Grary <grary.stimon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Lasse, > > Yes, that was exactly what I needed. > > I see I need to explore the specifics of mixing in (which maybe Matz''s > book underserves) -- in this case it was the self.include(base) > expression that was key. > > Finally, where attributes refer logically to the same kind of data, > but have different names, I have aliased them to allow the broadest > use of my version of your validation module. > > Thanks so much, > > Grar > > On Apr 1, 9:41 am, Lasse Bunk <lasseb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Grar, > > > > If you have a User model where you want to use some shared validations, > you > > could create a module like this – in lib/my_email_validations.rb: > > > > module MyEmailValidations > > def self.included(base) > > base.validates_presence_of :email > > base.validates_length_of :email, :minimum => 6 > > base.validate :email_must_be_from_valid_provider > > end > > > > # example of a custom validator > > def email_must_be_from_valid_provider > > errors.add :email, "must be from a valid provider" if > > email !~ /...@gmail.com$/ > > end > > end > > > > and in your User model, include them like this: > > > > class User < ActiveRecord::Base > > include MyEmailValidations > > end > > > > Does this solve your problem? > > > > /Lasse > > > > 2010/4/1 Grary <grary.sti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > > Hi, > > > > > Despite close reading of Matz''s Ruby book and Agile Web Dev. w. Rails, > > > I can''t figure out how to tackle the challenge of sharing validation > > > code across models with attributes in common. > > > > > In each of my models I''d like to validate the shared attributes with a > > > custom validator that includes Rails ActiveRecord::Validations class > > > methods and some of my own validations. My problem is 1) I don''t know > > > how to pass attribute references to my special method and 2) (mundane) > > > I can''t successfully call any ActiveRecord::Validations class methods > > > from within my custom method. > > > > > A bit of garbage here, I know, but any thoughts? Is this kind of > > > problem handled by use of certain gems? > > > > > Thanks, > > > > > Grar > > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm > . > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > <rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%252Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.