Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:15:46+00:00 2026-06-17T23:15:46+00:00

I have a validation happening during the input process like so: validates :solids, :numericality

  • 0

I have a validation happening during the input process like so:

validates :solids, :numericality => { :greater_than_or_equal_to => 0 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}

I want this condition to change based on the selected id of a drop down box.

I tried this in my model:

if self.lab_method_id == 1
validates :solids, :numericality => { :greater_than_or_equal_to => 0 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}
elsif
validates :solids, :numericality => { :greater_than_or_equal_to => 5 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}
end

However this returned the error on page load:

undefined method `key?’ for nil:NilClass

I figured this had to do with the fact that the validations in the model get loaded on page load and this an if statement checking for the id at that time would not work. Although this error doesn’t quite make sense to me.

So I am wondering how to set it up in a way that the specific validation is chosen based on the id of the drop down box item selected (am I even getting the id properly)?

Bonus: could someone explain the relationship to this particular error?

EDIT – stack trace:

vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/hide_actions.rb:36:in visible_action?'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/hide_actions.rb:18:in
method_for_action’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/implicit_render.rb:14:in method_for_action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/compatibility.rb:61:in
method_for_action’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:115:in process'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/abstract_controller/rendering.rb:45:in
process’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal.rb:203:in dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/rack_delegation.rb:14:in
dispatch’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal.rb:246:in block in action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:66:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:66:in dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:30:in
call’
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:68:in block in call'
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in
each’
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:589:in
call’
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:35:in block in call'
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in
catch’
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/best_standards_support.rb:17:in
call’
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/etag.rb:23:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/conditionalget.rb:25:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/head.rb:14:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/params_parser.rb:21:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/flash.rb:242:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in
context’
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/cookies.rb:338:in
call’
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/query_cache.rb:64:in call'
vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/callbacks.rb:28:in block in call'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in
run_803795940_call_68852270__callbacks’
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in __run_callback'
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in
_run_call_callbacks’
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in run_callbacks'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/callbacks.rb:27:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/reloader.rb:65:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/remote_ip.rb:31:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:16:in call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/show_exceptions.rb:56:in
call’
vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/rack/logger.rb:26:in call_app'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/rack/logger.rb:16:in
call’
config/initializers/quiet_assets.rb:6:in call_with_quiet_assets'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/request_id.rb:22:in
call’
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/runtime.rb:17:in
call’
vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/cache/strategy/local_cache.rb:72:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/lock.rb:15:in
call’
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/static.rb:53:in call'
vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/engine.rb:479:in
call’
vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/application.rb:220:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in
call’
vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/rack/log_tailer.rb:14:in call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in
service’
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in
run’
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:15:47+00:00Added an answer on June 17, 2026 at 11:15 pm

    NumericalityValidator (the underlying validator for this helper) doesn’t work with dynamic conditions as you want them. Use a custom validator:

    validate :solids_validation
    
    def solids_validation
      if self.lab_method_id
        lab_method = LabMethod.find(self.lab_method_id)
        if lab_method.solids?
          if (val = Kernel.Float(solids) rescue nil)
            min = (self.lab_method_id == 1 ? 0 : 5)
            errors.add(:solids, "must be greater than or equal to #{min}") if val < min
          else
            errors.add(:solids, :not_a_number)
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text box that I would like to do some validation on.
I have some validation on a text input bound to its blur event. I
I have code like below to perform some conditional validation on fields in my
I have the following in my ActiveRecord validation: validates :username, :exclusion => { :in
I would like my Eclipselink 2.3 Marshaller to perform validation upon marshalling. I have
I have the validation for autocomplete working. http://skoizumi.com/autocomplete/index.html var ac = $( #searchtext ).autocomplete({
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have set validation attributes in the model of my WCF service [MinLength(6, ErrorMessage
i have a validation in my .net textbox where it will take only numbers
i have a validation in my .net textbox where it will take only numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.