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@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.

Search Discussions

  • Frederick Cheung at Apr 23, 2012 at 2:53 am

    On Apr 23, 6:46 am, John Merlino 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@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.
  • John Merlino at Apr 30, 2012 at 4:15 pm
    yeah i was using rails 2 thanks for response
    On Apr 22, 10:53 pm, Frederick Cheung wrote:
    On Apr 23, 6:46 am, John Merlino 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 22, '12 at 10:47p
activeApr 30, '12 at 4:15p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

John Merlino: 2 posts Frederick Cheung: 1 post

People

Translate

site design / logo © 2021 Grokbase