Ruby on Rails Monday, April 30, 2012

yeah i was using rails 2 thanks for response

On Apr 22, 10:53 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> On Apr 23, 6:46 am, John Merlino <stoici...@aol.com> 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@googlegroups.com.
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.

No comments:

Post a Comment