nimpa
nimpa

Reputation: 25

Add validation to prevent out of range for ActiveRecord::Type::Integer with limit 4

I am running into out of range for ActiveRecord::Type::Integer with limit 4 error when typing a number over 10 digits. I want to add a validation to prevent this error from popping up.

this is my model thus far

 def self.find_archived(tenant, time_range, filters={}, options={})
    archived_recording_table = Arel::Table.new(ArchivedRecording.table_name)

    # create duration clauses
    duration_clauses = filters.try(:[],:duration).try(:[],:durations).to_a.each_index.collect{|index|
      duration = filters[:duration][:durations][index]
      case filters[:duration][:operators][index].to_sym
      when :lt
        archived_recording_table[:duration].lt(duration)
      when :gt
        archived_recording_table[:duration].gt(duration)
      end
end

this is my controller

def index

      unless params[:searched] 
        @filters[:duration] ||= Hash.new
        @filters[:duration][:durations] = ['1']
        @filters[:duration][:operators] = [:gt]
      end

      if @filters.try(:[],:duration).try(:[],:durations)
        @filters[:duration][:durations].map!(&:to_i)
      end

      @recording_metadata = RecordingMetadata.find(@tenant, request.session_options[:id], (start_time..end_time), @filters, additional_options)
    end
  end

What i tried soo far to get validation to work in the model

  validates :duration, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: 9999999999 }

But I am still getting the same error. I read that changing the column to limit 8 would solve this but is there a workaround where I do not need to change the DB

stack trace



app/models/recording_metadata.rb:209:in `eval'
activerecord (4.2.9) lib/active_record/type/integer.rb:23:in `type_cast_for_database'
activerecord (4.2.9) lib/active_record/connection_adapters/abstract/quoting.rb:14:in `quote'
arel (6.0.3) lib/arel/visitors/to_sql.rb:771:in `quote'
arel (6.0.3) lib/arel/visitors/to_sql.rb:731:in `quoted'
arel (6.0.3) lib/arel/visitors/to_sql.rb:151:in `visit_Arel_Nodes_Casted'
arel (6.0.3) lib/arel/visitors/reduce.rb:13:in `visit'
arel (6.0.3) lib/arel/visitors/to_sql.rb:516:in `visit_Arel_Nodes_GreaterThan'
arel (6.0.3) lib/arel/visitors/reduce.rb:13:in `visit'
arel (6.0.3) lib/arel/visitors/reduce.rb:7:in `accept'
arel (6.0.3) lib/arel/nodes/node.rb:48:in `to_sql'
(eval):1:in `find_archived'
app/models/recording_metadata.rb:209:in `eval'
app/models/recording_metadata.rb:209:in `find_archived'
app/models/recording_metadata.rb:19:in `find'
app/controllers/recordings_controller.rb:67:in `index'
actionpack (4.2.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.9) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.9) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.9) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.9) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.9) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.9) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.9) lib/active_support/callbacks.rb:498:in `block (2 levels) in around'
activesupport (4.2.9) lib/active_support/callbacks.rb:313:in `block (2 levels) in halting'
app/controllers/application_controller.rb:103:in `lock_config_files'
activesupport (4.2.9) lib/active_support/callbacks.rb:432:in `block in make_lambda'
activesupport (4.2.9) lib/active_support/callbacks.rb:312:in `block in halting'
activesupport (4.2.9) lib/active_support/callbacks.rb:497:in `block in around'
activesupport (4.2.9) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.9) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.9) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.9) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.9) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.9) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
activeldap (4.0.4) lib/active_ldap/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.9) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.9) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.9) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.9) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.9) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.9) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.9) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.9) lib/action_dispatch/routing/route_set.rb:817:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
/home/nishantp/.bundle/ruby/2.3.0/rails-dev-boost-15ebc37b3f1b/lib/rails_development_boost/async.rb:12:in `block in call'
/home/nishantp/.bundle/ruby/2.3.0/rails-dev-boost-15ebc37b3f1b/lib/rails_development_boost/async.rb:39:in `block in synchronize'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/nishantp/.bundle/ruby/2.3.0/rails-dev-boost-15ebc37b3f1b/lib/rails_development_boost/async.rb:39:in `synchronize'
/home/nishantp/.bundle/ruby/2.3.0/rails-dev-boost-15ebc37b3f1b/lib/rails_development_boost/async.rb:12:in `call'
exception_notification (4.2.2) lib/exception_notification/rack.rb:41:in `call'
rack (1.6.8) lib/rack/etag.rb:24:in `call'
rack (1.6.8) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.8) lib/rack/head.rb:13:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.8) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.8) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.9) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.9) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.9) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.9) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.9) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.9) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.9) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.9) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.9) lib/rails/rack/logger.rb:20:in `call'
request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.8) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.8) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.9) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.8) lib/rack/lock.rb:17:in `call'
actionpack (4.2.9) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.8) lib/rack/sendfile.rb:113:in `call'
railties (4.2.9) lib/rails/engine.rb:518:in `call'
railties (4.2.9) lib/rails/application.rb:165:in `call'
rack (1.6.8) lib/rack/lock.rb:17:in `call'
rack (1.6.8) lib/rack/content_length.rb:15:in `call'
rack (1.6.8) lib/rack/handler/webrick.rb:88:in `service'
/usr/local/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/usr/local/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/usr/local/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

Upvotes: 0

Views: 1497

Answers (2)

spickermann
spickermann

Reputation: 106882

The max value of a 4-bytes integer column in a database is 2**31 - 1 == 2147483647.

Fo ensure that you cannot write values bigger than that to the database just change your validation to:

validates :duration, numericality: { only_integer: true, 
                                     greater_than: 0, 
                                     less_than:    2**31 }

But looking at your stack trace it feels like the issue is not that you are trying to store a value into the database but instead, you build a query that includes a value that is over that limit and therefore your database complains about an invalid filter.

A workaround might be to limit the maximum duration in the filter by changing this line

duration = filters[:duration][:durations][index]

to

duration = [filters[:duration][:durations][index], (2**31 - 1)].min

Upvotes: 1

Alok Swain
Alok Swain

Reputation: 6519

The max value for an integer with limit 4 is 2147483647. You will have to increase the limit of the column if you want to allow values greater than 2147483647 but less than or equal to 9999999999

Upvotes: 0

Related Questions