添加验证以防止超出活动记录的范围::类型::限制为 4 的整数



我遇到了超出活动记录的范围::类型::键入超过 10 位数字时限制为 4 的整数错误。我想添加一个验证以防止弹出此错误。

这是我到目前为止的模型

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

这是我的控制器

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

到目前为止,我试图让验证在模型中工作

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

但我仍然遇到同样的错误。我读到将列更改为限制 8 可以解决此问题,但是是否有一种解决方法,我不需要更改数据库

堆栈跟踪


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'

数据库中 4 字节整数列的最大值为2**31 - 1 == 2147483647

Fo 确保不能将大于该值的值写入数据库,只需将验证更改为:

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

但是查看堆栈跟踪,感觉问题不在于您尝试将值存储到数据库中,而是构建一个包含超过该限制的值的查询,因此您的数据库抱怨过滤器无效。

解决方法可能是通过更改此行来限制筛选器中的最长持续时间

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

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

限制为4 的整数的最大值为2147483647。如果要允许大于2147483647但小于或等于的值,则必须增加列的限制9999999999

相关内容

  • 没有找到相关文章

最新更新