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

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

yeah i was using rails 2 thanks for response