Topic: current_user on Active Model Serializer

I need to access the current user in a serializer. I read this post http://stackoverflow.com/questions/1166 … erializers . Did the same but with no luck.

Any suggestions?

Re: current_user on Active Model Serializer

Hello
I'm pretty sure you'll find the answer in this railscast http://railscasts.com/episodes/409-acti … erializers

Re: current_user on Active Model Serializer

actually i did follow that cast, but i have a few diferences, im using cache, and i cache all the json generated. Thous, my code is like this.

 
module Cacheable
  extend ActiveSupport::Concern

  def to_json
    Rails.cache.fetch self.cache_key do
      self.active_model_serializer.new(self).to_json
    end
  end

  module ClassMethods

    # Cache key for tables
    def cache_key
      ActiveSupport::Cache.expand_cache_key "#{scoped.maximum(:updated_at).try(:to_i)}-#{scoped.count}"
    end

  end
end

i have seen in a place that this was used when declaring explisitely the active_model_serializer.

 self.active_model_serializer.new(self, :serialization_scope = > current_user).to_json 

but did not quite work either.

Im using Devise, and my ApplicationController stats like this. In the cast Ryan uses a stub and a helper method, suposingly the current_user is accesible in any controller (because of a custom helper that Devise provides). I dont quite get what i am missing.

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale
  before_filter :authenticate_user!

Cheers!!!