Active_model_serializers: undefined local variable or method `current_user' for UserSerializer

Created on 27 May 2013  Â·  11Comments  Â·  Source: rails-api/active_model_serializers

It would appear that, for whatever reason, my Serializers don't have access to the current_user.

Here's my UserSerializer. Emails are private on my site, so:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :username, :name, :email, :city, :region, :country, :latitude, :longitude

  def include_email?
    object == current_user
  end
end

Here's my declaration of current_user in my ApplicationController:

class ApplicationController < ActionController::API
  protected
    def current_user
      # Authorization: token TOKEN
      header = request.headers['Authorization'].split(' ')

      token = if header.present? && header.first.downcase == 'token'
                headers.last
              else
                params[:auth_token]
              end

      @current_user ||= User.find_by(auth_token: token)
    end
end

However, attempting to call render_json: @user from a controller action enters the serializer and blows up inside #include_email?:

undefined local variable or method `current_user' for #<UserSerializer:0x007fceb81e14a8>

App backtrace
-------------

 - app/serializers/user_serializer.rb:5:in `include_email?'
 - app/controllers/api/v1/users_controller.rb:4:in `show'

Full backtrace
--------------

 - app/serializers/user_serializer.rb:5:in `include_email?'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:294:in `include?'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:378:in `block in attributes'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:377:in `each'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:377:in `attributes'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:279:in `block in serializable_hash'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/notifications.rb:161:in `instrument'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:395:in `instrument'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:278:in `serializable_hash'
 - (gem) active_model_serializers-0.7.0/lib/active_model/serializer.rb:266:in `as_json'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/json/encoding.rb:50:in `block in encode'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/json/encoding.rb:49:in `encode'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/json/encoding.rb:34:in `encode'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/renderers.rb:90:in `block in <module:Renderers>'
 - (gem) active_model_serializers-0.7.0/lib/action_controller/serialization.rb:66:in `_render_option_json'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/renderers.rb:33:in `block in _handle_render_options'
 - /Users/david/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/set.rb:232:in `each_key'
 - /Users/david/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/set.rb:232:in `each'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/renderers.rb:30:in `_handle_render_options'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/renderers.rb:26:in `render_to_body'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/rendering.rb:97:in `render'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/rendering.rb:16:in `render'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
 - /Users/david/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/core_ext/benchmark.rb:12:in `ms'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:41:in `block in render'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
 - (gem) activerecord-4.0.0.rc1/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:40:in `render'
 - app/controllers/api/v1/users_controller.rb:4:in `show'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/base.rb:189:in `process_action'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/rendering.rb:10:in `process_action'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/callbacks.rb:392:in `_run__4242494627520311073__process_action__callbacks'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/callbacks.rb:80:in `run_callbacks'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/callbacks.rb:17:in `process_action'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/rescue.rb:29:in `process_action'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/notifications.rb:159:in `block in instrument'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/notifications.rb:159:in `instrument'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
 - (gem) activerecord-4.0.0.rc1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/base.rb:136:in `process'
 - (gem) actionpack-4.0.0.rc1/lib/abstract_controller/rendering.rb:44:in `process'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal.rb:195:in `dispatch'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 - (gem) actionpack-4.0.0.rc1/lib/action_controller/metal.rb:231:in `block in action'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:80:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:48:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/routing/mapper.rb:44:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/journey/router.rb:71:in `block in call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/journey/router.rb:59:in `each'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/journey/router.rb:59:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/routing/route_set.rb:654:in `call'
 - (gem) newrelic_rpm-3.6.2.96/lib/new_relic/rack/error_collector.rb:12:in `call'
 - (gem) newrelic_rpm-3.6.2.96/lib/new_relic/rack/agent_hooks.rb:18:in `call'
 - (gem) newrelic_rpm-3.6.2.96/lib/new_relic/rack/developer_mode.rb:28:in `call'
 - (gem) rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
 - /Users/david/Documents/Code/goodbrews/api/vendor/bundle/bundler/gems/api-versions-069d9d24d621/lib/api-versions/middleware.rb:22:in `call'
 - (gem) rack-1.5.2/lib/rack/etag.rb:23:in `call'
 - (gem) rack-1.5.2/lib/rack/conditionalget.rb:25:in `call'
 - (gem) rack-1.5.2/lib/rack/head.rb:11:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - (gem) activerecord-4.0.0.rc1/lib/active_record/query_cache.rb:36:in `call'
 - (gem) activerecord-4.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
 - (gem) activerecord-4.0.0.rc1/lib/active_record/migration.rb:366:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/callbacks.rb:392:in `_run__132673760723179563__call__callbacks'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/callbacks.rb:80:in `run_callbacks'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/reloader.rb:64:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:84:in `protected_app_call'
 - (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:79:in `better_errors_call'
 - (gem) better_errors-0.8.0/lib/better_errors/middleware.rb:56:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - (gem) railties-4.0.0.rc1/lib/rails/rack/logger.rb:38:in `call_app'
 - (gem) railties-4.0.0.rc1/lib/rails/rack/logger.rb:21:in `block in call'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/tagged_logging.rb:67:in `block in tagged'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/tagged_logging.rb:25:in `tagged'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/tagged_logging.rb:67:in `tagged'
 - (gem) railties-4.0.0.rc1/lib/rails/rack/logger.rb:21:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - (gem) rack-1.5.2/lib/rack/runtime.rb:17:in `call'
 - (gem) activesupport-4.0.0.rc1/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 - (gem) rack-1.5.2/lib/rack/lock.rb:17:in `call'
 - (gem) actionpack-4.0.0.rc1/lib/action_dispatch/middleware/static.rb:64:in `call'
 - (gem) railties-4.0.0.rc1/lib/rails/engine.rb:511:in `call'
 - (gem) railties-4.0.0.rc1/lib/rails/application.rb:96:in `call'
 - (gem) rack-1.5.2/lib/rack/content_length.rb:14:in `call'
 - (gem) puma-2.0.1/lib/puma/server.rb:364:in `handle_request'
 - (gem) puma-2.0.1/lib/puma/server.rb:243:in `process_client'
 - (gem) puma-2.0.1/lib/puma/server.rb:142:in `block in run'
 - (gem) puma-2.0.1/lib/puma/thread_pool.rb:92:in `call'
 - (gem) puma-2.0.1/lib/puma/thread_pool.rb:92:in `block in spawn_thread'

All 11 comments

Use scope instead of current_user as described here https://github.com/rails-api/active_model_serializers/blob/8795f2bc1eccf4c4a26ce8853af0a2e58fedd45a/lib/active_model/serializer.rb#L38.
Let me know if it works :).

We may need to fix the README to explain that, if you want to contribute with that please go ahead :).
Thanks.

Hey @spastorino, thanks for pointing me to that. I can get it to work now, but only by manually using the scope: current_user option in every call to render :json; I can't seem to get any sort of serialization_scope functionality to work either. The default :current_user scope isn't being picked up from the serializers, or are explicit declarations of serialization_scope :current_user. The only way I've gotten this to work is manually passing in the current_user to every call to render :json

I can stick a binding.pry in a controller action and, every time, serialization_scope is nil for me. Can't figure out why.

You need to do ...

class ApplicationController < ActionController::Base
  serialization_scope :current_user
end

Like I said, I've tried using that explicit declaration both in ApplicationController and on a per-controller basis. Even then, for some reason, it's not setting the variable and serialization_scope ends up returning nil from Pry. I'm using rails-api with Rails 4.0.0.rc1

The application in question is at goodbrews/api. I'll probably also try a bit later with a fresh application.

Ahh yeah, sorry about that. Didn't realize you were already doing that.
Another thing https://github.com/goodbrews/api/blob/master/Gemfile#L12 can you test using the latest version?.
Let me know what happens.

Oh jeez, I didn't even realize there was an update, let alone that I was still restricting myself to 0.7.x. Sorry about this, I totally should have caught that. All working now!

Not sure if this is in 0.9.x as well (it wasn't clear from the documentation).

Can I access current_user directly in 0.9.x (instead of using scope)? It seems to give me an error but not sure if I was doing it incorrectly, or the feature never made it to 0.9.x branches?

Sorry if off topic, but I just added a current_user method to application_controller, and it seems, its getting called from:

active_model_serializers-0.10.6/lib/action_controller/serialization.rb:30:in `serialization_scope'",

I dont have any code like : serialization_scope :current_user

is this default now?

I am actually raising an exception when the correct authentication header is not sent, so I dont really want current_user to always be called.

I might have time to investigate the gem code unless I am just doing it wrong.

Using serializers with Rails 5.1.4

active_model_serializers (0.10.6)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.2)

from Gemfile.lock

This is described in the docs. Set serialization_scope nil or IIRC

On Wed, Jan 24, 2018 at 7:13 AM, Brent Greeff notifications@github.com
wrote:

Sorry if off topic, but I just added a current_user method to
application_controller, and it seems, its getting called from:

active_model_serializers-0.10.6/lib/action_controller/serialization.rb:30:in
`serialization_scope'",

I dont have any code like : serialization_scope :current_user

is this default now?

I am actually raising an exception when the correct authentication header
is not sent, so I dont really want current_user to always be called.

I might have time to investigate the gem code unless I am just doing it
wrong.

Using serializers with Rails 5.1.4

active_model_serializers (0.10.6)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.2)

from Gemfile.lock

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/rails-api/active_model_serializers/issues/315#issuecomment-360128216,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAIuQmDctuXqpLZ1SBAWjcBOftx-wAuwks5tNyyQgaJpZM4Arxtt
.

Was this page helpful?
0 / 5 - 0 ratings