ruby-on-rails – rails 4 activerecord TypeError nil不是符号

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – rails 4 activerecord TypeError nil不是符号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看来sql server中的表是在select语句中放置nil.我不知道如何解决这个问题,如果我切换到任何其他表,我可以很容易地获得第一个记录.还有其他人处理过这个bug吗?我把桌子分开并构建了一个视图.它仍然这样做.猴子修补这个犹豫不决.想法?我是如何连接到sql Server的:

我的模特:

class Deal < ActiveRecord::Base
  self.table_name = 'vwStorePromosExtSummaryGlobal'
end

我的控制器:

class V1::DealsController < ApplicationController
    def by_mall_id
      deals = Deal.where(mallid: params[:id])
      render json: deals
    end
end

# Use sqlserver as the database for Active Record
gem 'tiny_tds','~> 0.6.1'
gem 'activerecord-sqlserver-adapter',github: 'arthrex/activerecord-sqlserver-adapter'
gem 'active_model_serializers','~> 0.8.1'

当我打开宝石中的确切错误时,我看到:

97:     def serializable_hash(options = nil)
     98:       options ||= {}
     99:
    100:       attribute_names = attributes.keys
    101:       if only = options[:only]
    102:         attribute_names &= Array(only).map(&:to_s)
    103:       elsif except = options[:except]
    104:         attribute_names -= Array(except).map(&:to_s)
    105:       end
    106:
    107:       hash = {}
    108:
 => 109: binding.pry
    110:
    111:       attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) }
    112:
    113:       Array(options[:methods]).each { |m| hash[m.to_s] = send(m) if respond_to?(m) }
    114:
    115:       serializable_add_includes(options) do |association,records,opts|
    116:         hash[association.to_s] = if records.respond_to?(:to_ary)
    117:           records.to_ary.map { |a| a.serializable_hash(opts) }
    118:         else
    119:           records.serializable_hash(opts)
    120:         end
    121:       end
    122:
    123:       hash
    124:     end

[1] pry(#<Deal>)> p attribute_names.each
#<Enumerator: ["mallid",nil]:each>
=> #<Enumerator: ...>
[2] pry(#<Deal>)> p attribute_names
["mallid",nil]
=> ["mallid",nil]
[3] pry(#<Deal>)>

这是我的完整痕迹:

[1m[36msql (49.6ms)[0m  [1mUSE [pcmenu][0m
Processing by V1::DealsController#by_mall_id as JSON
  Parameters: {"id"=>"1066"}
  [1m[35mDeal Load (44.5ms)[0m  EXEC sp_executesql N'SELECT TOP (1) mallid FROM [StorePromosExtSummaryGlobal] WHERE [StorePromosExtSummaryGlobal].[mallid] = N''1066'' AND (salestypeid = 11
 AND calcendDate > ''09-09-2013''
 AND displayDate < ''09-09-2013''
 AND (promoType is null or promoType = 0)) ORDER BY displayName ASC'
Completed 500 Internal Server Error in 546ms

TypeError - nil is not a symbol:
  activemodel (4.0.0) lib/active_model/serialization.rb:108:in `block in serializable_hash'
  activemodel (4.0.0) lib/active_model/serialization.rb:108:in `serializable_hash'
  activerecord (4.0.0) lib/active_record/serialization.rb:17:in `serializable_hash'
  activemodel (4.0.0) lib/active_model/serializers/json.rb:99:in `as_json'
  activesupport (4.0.0) lib/active_support/json/encoding.rb:50:in `block in encode'
  activesupport (4.0.0) lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
  activesupport (4.0.0) lib/active_support/json/encoding.rb:49:in `encode'
  activesupport (4.0.0) lib/active_support/json/encoding.rb:34:in `encode'
  activesupport (4.0.0) lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
  actionpack (4.0.0) lib/action_controller/Metal/renderers.rb:90:in `block in <module:Renderers>'
  active_model_serializers (0.8.1) lib/action_controller/serialization.rb:48:in `_render_option_json'
  actionpack (4.0.0) lib/action_controller/Metal/renderers.rb:33:in `block in _handle_render_options'
  /Users/chrishough/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/set.rb:232:in `each'
  actionpack (4.0.0) lib/action_controller/Metal/renderers.rb:30:in `_handle_render_options'
  actionpack (4.0.0) lib/action_controller/Metal/renderers.rb:26:in `render_to_body'
  actionpack (4.0.0) lib/abstract_controller/rendering.rb:97:in `render'
  actionpack (4.0.0) lib/action_controller/Metal/rendering.rb:16:in `render'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/chrishough/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
  activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:41:in `block in render'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:40:in `render'
  app/controllers/v1/deals_controller.rb:6:in `by_mall_id'
  apipie-rails (0.0.22) lib/apipie/dsl_definition.rb:193:in `block in _apipie_define_validators'
  actionpack (4.0.0) lib/action_controller/Metal/implicit_render.rb:4:in `send_action'
  actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.0.0) lib/action_controller/Metal/rendering.rb:10:in `process_action'
  actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (4.0.0) lib/active_support/callbacks.rb:383:in `_run__408012802371174283__process_action__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.0) lib/action_controller/Metal/rescue.rb:29:in `process_action'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.0) lib/action_controller/Metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.0) lib/action_controller/Metal/params_wrapper.rb:245:in `process_action'
  activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.0) lib/action_controller/Metal.rb:195:in `dispatch'
  actionpack (4.0.0) lib/action_controller/Metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.0) lib/action_controller/Metal.rb:231:in `block in action'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
  apipie-rails (0.0.22) lib/apipie/static_dispatcher.rb:56:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__980017763409925121__call__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  puma (2.5.1) lib/puma/server.rb:472:in `handle_request'
  puma (2.5.1) lib/puma/server.rb:343:in `process_client'
  puma (2.5.1) lib/puma/server.rb:242:in `block in run'
  puma (2.5.1) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

解决方法

虽然我仍然认为这是活动记录中的一个错误,但我能够用 https://github.com/rails-api/active_model_serializers宝石来解决它.这个gem覆盖了to_json,无需调用活动记录中的bug代码.

还有其他人有更好的答案吗?

更新控制器:

class V1::DealsController < ApplicationController

    def by_mall_id
      deals = Deal.where(mallid: params[:id]).first
      render json: deals,serializer: V1::DealSerializer
    end

end

我的新DealSerializer:

class V1::DealSerializer < ActiveModel::Serializer
    attributes :MALLID

    def MALLID
      object.mallid
    end

end

猜你在找的Ruby相关文章