I use serializer for rendering all records as json, but got these exceptions. How can I fixed this issue?
@manufacturers = Manufacturer.all
render json: V1::ManufacturerSerializer.new(@manufacturers).serializable_hash
NoMethodError - undefined method `read_attribute_for_serialization' for #<Manufacturer::ActiveRecord_Relation:0x007fb2db9b5d58>:
activerecord (5.0.0.beta1) lib/active_record/relation/delegation.rb:123:in `method_missing'
activerecord (5.0.0.beta1) lib/active_record/relation/delegation.rb:93:in `method_missing'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:88:in `block (2 levels) in attributes'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:160:in `block in attributes'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:159:in `each'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:159:in `each_with_object'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:159:in `attributes'
app/serializers/v1/base_serializer.rb:3:in `attributes'
active_model_serializers (0.9.3) lib/active_model/serializer.rb:284:in `serializable_object'
app/controllers/api/v1/manufacturers_controller.rb:15:in `index'
actionpack (5.0.0.beta1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.beta1) lib/abstract_controller/base.rb:183:in `process_action'
actionpack (5.0.0.beta1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.beta1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.beta1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.beta1) lib/action_controller/metal/rescue.rb:27:in `process_action'
actionpack (5.0.0.beta1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (5.0.0.beta1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.beta1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.beta1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.beta1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (5.0.0.beta1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.beta1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.beta1) lib/abstract_controller/base.rb:128:in `process'
actionpack (5.0.0.beta1) lib/action_controller/metal.rb:192:in `dispatch'
actionpack (5.0.0.beta1) lib/action_controller/metal.rb:264:in `dispatch'
actionpack (5.0.0.beta1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.beta1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.beta1) lib/action_dispatch/journey/router.rb:42:in `block in serve'
actionpack (5.0.0.beta1) lib/action_dispatch/journey/router.rb:29:in `each'
actionpack (5.0.0.beta1) lib/action_dispatch/journey/router.rb:29:in `serve'
actionpack (5.0.0.beta1) lib/action_dispatch/routing/route_set.rb:715:in `call'
actionview (5.0.0.beta1) lib/action_view/digestor.rb:14:in `call'
warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
warden (1.2.4) lib/warden/manager.rb:34:in `catch'
warden (1.2.4) lib/warden/manager.rb:34:in `call'
rack (2.0.0.alpha) lib/rack/etag.rb:25:in `call'
rack (2.0.0.alpha) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.0.alpha) lib/rack/head.rb:12:in `call'
rack (2.0.0.alpha) lib/rack/session/abstract/id.rb:220:in `context'
rack (2.0.0.alpha) lib/rack/session/abstract/id.rb:214:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/cookies.rb:608:in `call'
activerecord (5.0.0.beta1) lib/active_record/query_cache.rb:36:in `call'
activerecord (5.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:963:in `call'
activerecord (5.0.0.beta1) lib/active_record/migration.rb:524:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.beta1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/reloader.rb:71:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/remote_ip.rb:79: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 (5.0.0.beta1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.0.0) lib/web_console/middleware.rb:27:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.beta1) lib/rails/rack/logger.rb:42:in `call_app'
railties (5.0.0.beta1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.beta1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.beta1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.beta1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.beta1) lib/rails/rack/logger.rb:24:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.0.alpha) lib/rack/method_override.rb:22:in `call'
rack (2.0.0.alpha) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.beta1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/load_interlock.rb:13:in `call'
actionpack (5.0.0.beta1) lib/action_dispatch/middleware/static.rb:132:in `call'
rack (2.0.0.alpha) lib/rack/sendfile.rb:111:in `call'
honeybadger (2.3.2) lib/honeybadger/rack/error_notifier.rb:33:in `block in call'
honeybadger (2.3.2) lib/honeybadger/config.rb:190:in `with_request'
honeybadger (2.3.2) lib/honeybadger/rack/error_notifier.rb:30:in `call'
honeybadger (2.3.2) lib/honeybadger/rack/user_feedback.rb:28:in `call'
honeybadger (2.3.2) lib/honeybadger/rack/user_informer.rb:18:in `call'
railties (5.0.0.beta1) lib/rails/engine.rb:522:in `call'
puma (2.15.3) lib/puma/server.rb:541:in `handle_request'
puma (2.15.3) lib/puma/server.rb:388:in `process_client'
puma (2.15.3) lib/puma/server.rb:270:in `block in run'
puma (2.15.3) lib/puma/thread_pool.rb:106:in `block in spawn_thread'
Thanks for the issue!
Please add some more information per https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md
Also, what happens if you call to_a at the end of @manufacturers? i.e. manufacturers = Manufacturer.all.to_a
Also, what happens if you call to_a at the end of @manufacturers?
--> It returns an Ruby array
Bellow is the exception when I call to_json method:
[12] pry(main)> manufacturers = Manufacturer.all
[12] pry(main)> se = V1::ManufacturerSerializer.new(manufacturers)
=> #<V1::ManufacturerSerializer:0x007fd888ec4268
@context=nil,
@except=nil,
@key_format=nil,
@meta=nil,
@meta_key=:meta,
@namespace=nil,
@object=
[#<Manufacturer:0x007fd88e8b1870
id: 1,
name: "Cocacola Vietnam",
description: "Cocacola Vietnam",
created_at: Tue, 05 Jan 2016 10:55:05 ICT +07:00,
updated_at: Tue, 05 Jan 2016 10:55:05 ICT +07:00>],
@only=nil,
@polymorphic=false,
@root=nil,
@scope=nil,
@wrap_in_array=nil>
[13] pry(main)> se.to_json
NoMethodError: undefined method `read_attribute_for_serialization' for #<Manufacturer::ActiveRecord_Relation:0x007fd88aae1e00>
from /Users/hoanghoa/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.beta1/lib/active_record/relation/delegation.rb:123:in `method_missing'
@hoahm please respond to my requests in https://github.com/rails-api/active_model_serializers/issues/1411#issuecomment-168895314
@bf4 I'm having the same issue. As much as I understand what supporting OS means (I do too), I have to say that the CONTRIBUTING.md is likely the most off putting I've seen. I will try to offer steps to reproduce but won't spend 30+mn reading this enormous list of contribution requirements and answer them all. It is likely overkill anyway.
Back to the matter, here is how we serialize record or collection to json:
def serialize_record(
record_or_collection,
serializer_class: nil
)
serializer_class ||= ActiveModel::Serializer.serializer_for(record_or_collection)
serializer = serializer_class.new(record_or_collection)
ActiveModel::Serializer.adapter.new(serializer).as_json
end
If record_or_collection is a single record, all is good. If record_or_collection is a collection it breaks. Obviously, record_or_collection.to_a return an array of the collection, but still fails.
We are using ruby 2.2.3, Rails 4.2.5 and the version 0.10.0.rc3 of the gem.
It is not clear to me if ActiveModel::Serializer is even the right way to serialize collections.
Thanks for the support ;)
As much as I understand what supporting OS means (I do too), I have to say that the CONTRIBUTING.md is likely the most off putting I've seen. I will try to offer steps to reproduce but won't spend 30+mn reading this enormous list of contribution requirements and answer them all. It is likely overkill anyway.
@karellm I'd love your assistance in making the CONTRIBUTING.md more friendly :)
(btw, try rc4)
I'd write the serialization as
ActiveModel::SerializableResource.new(record_or_collection).as_json
You could pass in the serializer as an option there, as well.
Also, this issue appears to be for 0.9 and you're on 0.10. If what I wrote is helpful, would you mind opening a new one? Thanks!
Thanks for that, I confirm it works great (at least with rc4). I don't know why I missed something that obvious. For future reference, I would add that you can pass either serializer for a record and each_serializer for a collection:
# default serializer
ActiveModel::SerializableResource.new(record_or_collection).as_json
# custom serializer
ActiveModel::SerializableResource.new(record, serializer: CustomSerializer).as_json
ActiveModel::SerializableResource.new(collection, each_serializer: CustomSerializer).as_json
@bf4 I'm not sure what you'd like me to do when you say If what I wrote is helpful, would you mind opening a new one?? Open a new ticket and close it since it is resolved?
As to the CONTRIBUTING contribution, it is actually friendly and the content is good! The main issue is the length and the fact it is hard to read. If you could shrink the writing section to a third of its size, it would help a lot I think. Since I'm not familiar with this project, I'm not sure I'm the best person to do it though.
@karellm Great!. oops If what I wrote is helpful to If what I wrote isn't helpful
Since I'm not familiar with this project, I'm not sure I'm the best person to do it though.
Or maybe that _does_ makes you the best :)
Most helpful comment
Thanks for that, I confirm it works great (at least with rc4). I don't know why I missed something that obvious. For future reference, I would add that you can pass either
serializerfor a record andeach_serializerfor a collection:@bf4 I'm not sure what you'd like me to do when you say
If what I wrote is helpful, would you mind opening a new one?? Open a new ticket and close it since it is resolved?As to the CONTRIBUTING contribution, it is actually friendly and the content is good! The main issue is the length and the fact it is hard to read. If you could shrink the writing section to a third of its size, it would help a lot I think. Since I'm not familiar with this project, I'm not sure I'm the best person to do it though.