所以我使用Rails 4.2.0的Ruby 2.1.3并且我使用active_model_serializer gem将适当的json数据发送到AngularJs。
今天我没有从Rails 4.1.6升级到4.2.0。对于以前的版本,问题不存在。
当我尝试从我的媒体节目动作中获取json时出现问题:
def show
medium = Medium.find(params[:id])
respond_with medium, include: [{comments: :user}, :likes, :user]
end
这就是我在升级rails之前看到的相同动作:
def show
medium = Medium.includes([{comments: :user}, :likes, :user]).find(params[:id])
respond_with medium
end
过去能够以旧的方式工作,但是使用新的rails,它在发送的json数据中不再包含带有媒体的ActiveModel。
所以我使用此页面上的示例更改了代码:https://github.com/rails-api/active_model_serializers
以下是我的媒体序列化程序的样子:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
end
在升级之前,它看起来像这样:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
def include_user?
object.association(:user).loaded?
end
def include_comments?
object.association(:comments).loaded?
end
def include_associations?
object.association(:user, :comments, :likes).loaded?
end
end
因此,在对该show动作进行查询时,我的Rails应用程序抛出的错误是:
Started GET "/media/7.json" for 127.0.0.1 at 2014-12-25 18:13:43 +0200
Processing by Content::MediaController#show as JSON
Parameters: {"id"=>"7"}
Medium Load (0.4ms) SELECT "media".* FROM "media" WHERE "media"."id" = $1 LIMIT 1 [["id", 7]]
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."medium_id" = $1 [["medium_id", 7]]
Completed 500 Internal Server Error in 59ms
TypeError - can't clone Symbol:
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:77:in `try!'
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:63:in `try'
activerecord (4.2.0) lib/active_record/serialization.rb:12:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block (2 levels) in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:158:in `block in serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:156:in `serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:112:in `serializable_hash'
activerecord (4.2.0) lib/active_record/serialization.rb:17:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serializers/json.rb:99:in `as_json'
app/models/medium.rb:32: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>'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:45:in `block in _render_to_body_with_renderer'
/home/kaspar/.rbenv/versions/2.1.3/lib/ruby/2.1.0/set.rb:263: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.1.3/lib/ruby/2.1.0/benchmark.rb:294: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.0.2) lib/action_controller/responder.rb:258:in `display'
responders (2.0.2) lib/action_controller/responder.rb:212:in `api_behavior'
responders (2.0.2) lib/action_controller/responder.rb:191:in `rescue in to_format'
responders (2.0.2) lib/action_controller/responder.rb:185:in `to_format'
responders (2.0.2) lib/action_controller/responder.rb:163:in `respond'
responders (2.0.2) lib/action_controller/responder.rb:156:in `call'
responders (2.0.2) lib/action_controller/respond_with.rb:203:in `respond_with'
app/controllers/content/media_controller.rb:17:in `show'
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:151:in `block in halting_and_conditional'
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 `block in halting_and_conditional'
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 `block in halting'
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 `block in halting'
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 `_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 `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 `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 `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) 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.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) 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 `_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.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.0) 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.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) 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.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) 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 (4.0.56) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.56) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
值得注意的是,如果我从新的include:
方法中删除了两个:user include,那么一切正常。但我希望每个评论还包括创建它的用户和媒体,也包括上传它的用户。
这可能是什么问题?如果需要更多信息,请告诉我。
编辑:问题似乎只适用于:user
的{{1}}。
在此处发布我的评论序列化程序:
comments:
答案 0 :(得分:4)
我使用内置的ActiveModel序列化程序为JSON遇到了同样的错误。您可以使用与respond_with
电话类似的语法覆盖模型as_json
method。使用你的模型
include: [{comments: :user} ... ]
当我需要的是
include: [{comments: {include: [:user]}}, ... ]
查看以下更改是否解决了您的问题(如果您仍然拥有它)。
更改
respond_with medium, include: [{comments: :user}, :likes, :user]
到
respond_with medium, include: [{comments: {include: [:user]}}, :likes, :user]