无法获取序列化器来序列化has_many关联

时间:2015-05-15 19:45:48

标签: ruby-on-rails ruby json serialization active-model-serializers

所以我使用的是最新的active_model_serializer,我正在尝试使用它们在Medium上发送数组。

我用它来查询MediumMedium.includes([{comments: :user}, :user]).where(user_id: user_ids)

因此,媒体属于用户,媒体有很多评论,每条评论都有用户(所有者)。

现在这里是相应的序列化器:

中:

class MediumSerializer < ActiveModel::Serializer

    attributes :id,
               :text,
               :image_video_file_name,
               :image_video_croppable_url,
               :image_video_thumb_url,
               :image_video_big_url

    belongs_to :user
    has_many :comments


end

注释:

class CommentSerializer < ActiveModel::Serializer
    attributes :id,
               :text,
               :user_id,
               :medium_id

    belongs_to :user
end

用户:

class UserSerializer < ActiveModel::Serializer
    include Avatar

    attributes :id,
               :name,
               :email,
               :role,
               :avatar

    def avatar
        Medium.find(self.current_avatar)[0]
    end

    #has_one :avatar, serializer: AvatarSerializer
    has_many :media, :comments
end

我在Medium

中也有这些功能
def image_video_croppable_url
        image_video.url(:croppable)
    end

    def image_video_thumb_url
        image_video.url(:thumb)
    end

    def image_video_big_url
        image_video.url(:big)
    end

我使用Paperclip gem,我需要这些函数来访问不同媒体图像的大小。也许还有更好的方法。

所以现在当我进行查询时,我使用这一行将信息传递给浏览器,让序列化器很好地使它成为JSON:

respond_with media, each_serializer: MediumSerializer 

但它给了我这个错误:

NoMethodError - undefined method `image_video_file_name' for #<Comment:0x007f5a082a4310>:
  activemodel (4.2.0) lib/active_model/attribute_methods.rb:433:in `method_missing'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:37:in `block (2 levels) in attributes'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:200:in `block in attributes'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:198:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:198:in `each_with_object'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:198:in `attributes'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:22:in `block (3 levels) in serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter.rb:53:in `cache_check'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:21:in `block (2 levels) in serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/array_serializer.rb:5:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/array_serializer.rb:5:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:20:in `map'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:20:in `block in serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:224:in `call'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:224:in `block in each_association'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:208:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer.rb:208:in `each_association'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:17:in `serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:9:in `block in serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/array_serializer.rb:5:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/array_serializer.rb:5:in `each'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:9:in `map'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter/json.rb:9:in `serializable_hash'
  active_model_serializers (0.10.0.rc1) lib/active_model/serializer/adapter.rb:23:in `as_json'
  activesupport (4.2.0) lib/active_support/json/encoding.rb:34:in `encode'
  activesupport (4.2.0) lib/active_support/json/encoding.rb:21:in `encode'
  activesupport (4.2.0) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
  actionpack (4.2.0) lib/action_controller/metal/renderers.rb:116:in `block in <module:Renderers>'
  active_model_serializers (0.10.0.rc1) lib/action_controller/serialization.rb:49:in `block (2 levels) in <module:Serialization>'
  actionpack (4.2.0) lib/action_controller/metal/renderers.rb:45:in `block in _render_to_body_with_renderer'
  /home/kaspar/.rbenv/versions/2.2.0/lib/ruby/2.2.0/set.rb:283:in `each_key'
  /home/kaspar/.rbenv/versions/2.2.0/lib/ruby/2.2.0/set.rb:283:in `each'
  actionpack (4.2.0) lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
  actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
  actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /home/kaspar/.rbenv/versions/2.2.0/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
  activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
  responders (2.1.0) lib/action_controller/responder.rb:258:in `display'
  responders (2.1.0) lib/action_controller/responder.rb:212:in `api_behavior'
  responders (2.1.0) lib/action_controller/responder.rb:191:in `rescue in to_format'
  responders (2.1.0) lib/action_controller/responder.rb:185:in `to_format'
  responders (2.1.0) lib/action_controller/responder.rb:163:in `respond'
  responders (2.1.0) lib/action_controller/responder.rb:156:in `call'
  responders (2.1.0) lib/action_controller/respond_with.rb:203:in `respond_with'
  app/controllers/users_controller.rb:73:in `feed'
  actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.6.1) lib/rack/etag.rb:24:in `call'
  rack (1.6.1) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.1) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.1) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.1) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
  activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.1) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.1) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0) lib/rails/engine.rb:518:in `call'
  railties (4.2.0) lib/rails/application.rb:164:in `call'
  passenger (5.0.7) lib/phusion_passenger/rack/thread_handler_extension.rb:89:in `process_request'
  passenger (5.0.7) lib/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
  passenger (5.0.7) lib/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
  passenger (5.0.7) lib/phusion_passenger/request_handler.rb:414:in `block (3 levels) in start_threads'
  passenger (5.0.7) lib/phusion_passenger/utils.rb:111:in `block in create_thread_and_abort_on_exception'

当我尝试将Medium数组抛出到Serializer时,才会出现此错误。只有一个Medium我没有错误,结果如下:

Result

正如您所看到的,它具有我希望它在序列化程序中具有的所有属性,并且它设法将信息提取到所有属性中而没有任何问题。

为什么只有在对数组或相同对象执行相同操作时才会出现此问题?

0 个答案:

没有答案