John Merlino
2012-Apr-22 22:46 UTC
validating a date range based on current object''s attributes
Hey all, I look at the rails documentation validation helpers: http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html It has this example: class Person < ActiveRecord::Base validates_inclusion_of :states, :in => lambda{ |person| STATES[person.country] } end So basically when you update a record, that object instance is passed into the lambda and validation occurs. I try to emulate same thing: class Subject < ActiveRecord::Base validates_inclusion_of :screening, :in => lambda { |subject| (subject.screened_on + 7.days)..(subject.screened_on + 14.days) } end So basically I just want to make sure the date they entered is between dates the object was screend. However, I get this error: .rvm/gems/ruby-1.8.7-p357/gems/activerecord-2.3.10/lib/active_record/ validations.rb:908:in `validates_inclusion_of'': An object with the method include? is required must be supplied as the :in option of the configuration hash (ArgumentError) Ok so the source code shows this line: enum = configuration[:in] || configuration[:within] 607: 608: raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") Granted, a model instance does not have the include? method. So then why does the example in the rails documentation work and what can I do to emulate that? thanks for response -- 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.
Frederick Cheung
2012-Apr-23 02:53 UTC
Re: validating a date range based on current object''s attributes
On Apr 23, 6:46 am, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> Hey all, > > I look at the rails documentation validation helpers: > > http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMeth... > > It has this example: > > class Person < ActiveRecord::Base > validates_inclusion_of :states, :in => lambda{ |person| > STATES[person.country] } > end > > So basically when you update a record, that object instance is passed > into the lambda and validation occurs. >The ability to specify a lambda as a validates_inclusion_of target was added in 3.x (not exactly sure which version - I think 3.1) and you''re using 2.3.10, which doesn''t have this ability. You''ll probably need to write a custom validation, i.e. something along the lines of validate :check_screening_date def check_screening_date if ... errors.add(:screening, "screening must be between ...") end end Fred> I try to emulate same thing: > > class Subject < ActiveRecord::Base > validates_inclusion_of :screening, :in => lambda { |subject| > (subject.screened_on + 7.days)..(subject.screened_on + 14.days) } > end > > So basically I just want to make sure the date they entered is between > dates the object was screend. > > However, I get this error: > > .rvm/gems/ruby-1.8.7-p357/gems/activerecord-2.3.10/lib/active_record/ > validations.rb:908:in `validates_inclusion_of'': An object with the > method include? is required must be supplied as the :in option of the > configuration hash (ArgumentError) > > Ok so the source code shows this line: > > enum = configuration[:in] || configuration[:within] > 607: > 608: raise(ArgumentError, "An object with the method include? > is > required must be supplied as the :in option of the configuration > hash") > unless enum.respond_to?("include?") > > Granted, a model instance does not have the include? method. So then > why does the example in the rails documentation work and what can I do > to emulate that? > > thanks for response-- 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.
John Merlino
2012-Apr-30 16:15 UTC
Re: validating a date range based on current object''s attributes
yeah i was using rails 2 thanks for response On Apr 22, 10:53 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 23, 6:46 am, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: > > > > > > > > > > > Hey all, > > > I look at the rails documentation validation helpers: > > >http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMeth... > > > It has this example: > > > class Person < ActiveRecord::Base > > validates_inclusion_of :states, :in => lambda{ |person| > > STATES[person.country] } > > end > > > So basically when you update a record, thatobjectinstance is passed > > into the lambda and validation occurs. > > The ability to specify a lambda as a validates_inclusion_of target was > added in 3.x (not exactly sure which version - I think 3.1) and you''re > using 2.3.10, which doesn''t have this ability. You''ll probably need to > write a custom validation, i.e. something along the lines of > > validate :check_screening_date > > def check_screening_date > if ... > errors.add(:screening, "screening must be between ...") > end > end > > Fred > > > > > > > > > I try to emulate same thing: > > > class Subject < ActiveRecord::Base > > validates_inclusion_of :screening, :in => lambda { |subject| > > (subject.screened_on + 7.days)..(subject.screened_on + 14.days) } > > end > > > So basically I just want to make sure thedatethey entered is between > > dates theobjectwas screend. > > > However, I get this error: > > > .rvm/gems/ruby-1.8.7-p357/gems/activerecord-2.3.10/lib/active_record/ > > validations.rb:908:in `validates_inclusion_of'': Anobjectwith the > > method include? is required must be supplied as the :in option of the > > configuration hash (ArgumentError) > > > Ok so the source code shows this line: > > > enum = configuration[:in] || configuration[:within] > > 607: > > 608: raise(ArgumentError, "Anobjectwith the method include? > > is > > required must be supplied as the :in option of the configuration > > hash") > > unless enum.respond_to?("include?") > > > Granted, a model instance does not have the include? method. So then > > why does the example in the rails documentation work and what can I do > > to emulate that? > > > thanks for response-- 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.