edit: this is wrong. see my comment here on how to re-create.
I am trying to serialize an ActiveRecord_Relation.
users = User.select('LENGTH(email) as email_length, users.*')
render json: users
class UserSerializer < ActiveModel::Serializer
attributes :id, :email_length
end
and I get this error message:
undefined method `model_name' for User::ActiveRecord_Relation:Class
this is 0.10.0
what's the output of just users? like if if awesome_printed it?
@KevinColemanInc super important information is missing from this bug report: what did you do to get the error and the stack trace.
undefined methodmodel_name' for User::ActiveRecord_Relation:Class`
what were you doing that caused that issue. We're all busy people https://github.com/rails-api/active_model_serializers/blob/master/.github/ISSUE_TEMPLATE.md#steps-to-reproduce
Thanks
@bf4 @NullVoxPopuli Sorry about that! here is a better example:
Steps to reproduce
users = User.select('users.*').joins(:profile).group(:source)
class UserThreeSerializer < ActiveModel::Serializer
attributes :id
end
ActiveModelSerializers::SerializableResource.new(users, serializer: UserThreeSerializer).to_json
Rendered UserTwoSerializer with ActiveModelSerializers::Adapter::PaginatedJson (4.69ms)
NoMethodError: undefined method `model_name' for User::ActiveRecord_Relation:Class
So it seems like the problem happens when you try to serialize a query that groups the results.
User.all works fine
Rendered UserThreeSerializer with ActiveModelSerializers::Adapter::PaginatedJson (4.75ms)
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model/serializer.rb:175:in json_key'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/adapter/base.rb:51:inroot'
/Users/kcoleman/Sites/steadyfoot/lib/active_model_serializers/adapter/paginated_json.rb:12:in serializable_hash'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/adapter/base.rb:29:inas_json'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/json/encoding.rb:35:in encode'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/json/encoding.rb:22:inencode'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/core_ext/object/json.rb:37:in to_json_with_active_support_encoder'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/serializable_resource.rb:8:into_json'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:69:in block (3 levels) in notify'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:117:incall'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:555:in block (2 levels) in compile'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:incall'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:498:in block (2 levels) in around'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:343:inblock (2 levels) in simple'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:22:in block (3 levels) in instrument_rendering'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:79:inblock in notify_render'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in block in instrument'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:ininstrument'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in instrument'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:78:innotify_render'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:21:in block (2 levels) in instrument_rendering'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:95:inblock in tag_logger'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in block in tagged'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:26:intagged'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in tagged'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:95:intag_logger'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:20:in block in instrument_rendering'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:ininstance_exec'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:441:in block in make_lambda'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:342:inblock in simple'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:497:in block in around'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:incall'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in __run_callbacks__'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in_run_render_callbacks'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in run_callbacks'
/Users/kcoleman/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.0/lib/active_model_serializers/logging.rb:68:inblock (2 levels) in notify'
Thanks. So, the issue here is that you're serializing a collection (active relation) as if it were a single resource. For collections, you need to specify each_serializer: instead of serializer. I admit these options are a little confusing, but they've been around a while... (in this case, serializer: refers to the collection serializer. It makes sense, but doesn't behave how you'd expect out of the box)
Please let me know if this resolves your issue. If you think the docs or code could be improved with what you've learned, a PR would be most welcome.
thanks! I totally forgot about that.
As this issue arises a lot, and custom collection serializers do not seem to be used a lot, it might make sense to have the serializer option handle this automatically when called on a collection.
Most helpful comment
Thanks. So, the issue here is that you're serializing a collection (active relation) as if it were a single resource. For collections, you need to specify
each_serializer:instead ofserializer. I admit these options are a little confusing, but they've been around a while... (in this case,serializer:refers to the collection serializer. It makes sense, but doesn't behave how you'd expect out of the box)