Rails 报错 ‘NoMethodError (undefined method `sql_type' for "NUMBER":String)’

eric_chao · 2016年09月18日 · 最后由 eric_chao 回复于 2016年09月19日 · 1916 次阅读
NoMethodError (undefined method `sql_type' for "NUMBER":String):

app/controllers/strategy_backlogs_controller.rb:395:in `retrieve_strate_line_increment_ers'
app/controllers/strategy_backlogs_controller.rb:114:in `show'

Full Trace

activerecord (5.0.0.1) lib/active_record/connection_adapters/column.rb:8:in `sql_type'
activerecord (5.0.0.1) lib/active_record/connection_adapters/abstract/quoting.rb:63:in `lookup_cast_type_from_column'
activerecord (5.0.0.1) lib/active_record/model_schema.rb:359:in `block in load_schema!'
activerecord (5.0.0.1) lib/active_record/model_schema.rb:355:in `each'
activerecord (5.0.0.1) lib/active_record/model_schema.rb:355:in `load_schema!'
activerecord (5.0.0.1) lib/active_record/attributes.rb:233:in `load_schema!'
activerecord (5.0.0.1) lib/active_record/attribute_decorators.rb:28:in `load_schema!'
activerecord (5.0.0.1) lib/active_record/model_schema.rb:349:in `load_schema'
activerecord (5.0.0.1) lib/active_record/model_schema.rb:256:in `columns_hash'
activerecord (5.0.0.1) lib/active_record/querying.rb:41:in `find_by_sql'
app/controllers/strategy_backlogs_controller.rb:395:in `retrieve_strate_line_increment_ers'
app/controllers/strategy_backlogs_controller.rb:114:in `show'

报错地方的相关代码

for i in 0...in_size
  in_query.push("RPTNO in (#{all_bug_numbers[max_params_length*i...max_params_length*(i+1)].join(",")})")
end
bugs = Bug.find_by_sql("SELECT RPTNO, SUBJECT, STATUS, PRI_FOR_DEV, PROGRAMMER, CUSTOMER from RPTHEAD WHERE (#{in_query.join(' OR ')})")

升级到 rails5,遇到的问题

for i in 0...in_size
  # in_query.push("RPTNO in (#{all_bug_numbers[max_params_length*i...max_params_length*(i+1)].join(",")})")
  in_query.push("RPTNO in (#{all_bug_numbers[max_params_length*i...max_params_length*(i+1)].join(',')})")
end
puts "SELECT RPTNO, SUBJECT, STATUS, PRI_FOR_DEV, PROGRAMMER, CUSTOMER from RPTHEAD WHERE (#{in_query.join(' OR ')})"
bugs = Bug.find_by_sql("SELECT RPTNO, SUBJECT, STATUS, PRI_FOR_DEV, PROGRAMMER, CUSTOMER from RPTHEAD WHERE (#{in_query.join(' OR ')})")

OR

self.table_name = 'RPTHEAD'

rptnos = in_size.times.map do |i|
  all_bug_numbers[max_params_length*i...max_params_length*(i+1)] 
end.flatten

Bug.where(rptno: tptnos)

手写未验证。

问问题不是应该要明确下需求嘛?有很多 sql 封装的 gem 可以做这样的事

#2 楼 @flingfox63 因为很多页面都报这种错,没明白为什么报这种错。查找到对应的 gem---activerecord (5.0.0.1) 报错地方“delegate :precision, :scale, :limit, :type, :sql_type, to: :sql_type_metadata, allow_nil: true”

需要 登录 后方可回复, 如果你还没有账号请 注册新账号