active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

'cache' method undefined for ActiveModel::Serializer

Open wasaylor opened this issue 11 years ago • 9 comments

Trying to test AMS caching here. The class is missing the method all together. Any ideas? Using gem version 0.9.3. Cache config is redis_store

wasaylor avatar Mar 20 '15 04:03 wasaylor

same problem. there is an alternative. i can propose a more complex hook, if you trying to cache object in collection, with custom name serializer. for example:

users = User.all
users = users.map do |user|
  key = "UserShortSerializer-#{user.id}-#{user.updated_at.to_i}"
  Rails.cache.fetch(key, expires_in: 3.hours) do
    UserShortSerializer.new(user, meta: {...}, scope: view_context, root: false).attributes
  end
end

render json: {users: users, ...}

OR you can use 0.8 Anyone knows, 0.9 much more faster than 0.8/0.10? please correct me if i wrong

ipkes avatar Mar 20 '15 09:03 ipkes

Btw - I'm not set on 0.9.x. It happened to be the version installed by gem. I'm open to the best supported version. Just let me know which one that is. Thanks

wasaylor avatar Mar 23 '15 21:03 wasaylor

@wasaylor the greatest version will definitively be the 0.10.0 (it may become 1.0). The thing is that it still wasn't officially launched, but we are already conducting some productions tests with it. I'm not so sure if 0.8 would be a easy trade, but you should give it a try. You might also want to test 0.10.0, but use it carefully and help us by reporting if you find a bug.

joaomdmoura avatar Mar 24 '15 18:03 joaomdmoura

@wasaylor 0.10.0 is used by us in production and works stable.

hendricius avatar Jun 26 '15 12:06 hendricius

awesome! btw @ipkes 0.10 is faster then 0.9, I made some benchmarks, check it out on AMS talk at RailsConf

joaomdmoura avatar Jun 26 '15 17:06 joaomdmoura

After update to 0.10 i get an error saying wrong number of arguments (1 for 0) when calling render.

  def index
    @articles = SearchArticlesCommand.new(params).execute
    render json: @articles, status: :ok
  end

antarr avatar Aug 07 '15 23:08 antarr

Hey @atbyrd, I'm sorry to hear that, I'm not sure if you still have this problem. If you do, could you please open a new issue for that letting us know your Rails version and with you are use ams 0.10 rc1, rc2 or master?

joaomdmoura avatar Aug 12 '15 14:08 joaomdmoura

Just to be clear, is cached still not yet supported in 0.10.0.rc4

On that version I'm still getting:

NameError (undefined local variable or method 'cached' for PersonSerializer:Class):

app/serializers/person_serializer.rb:2:in '<class:PersonSerializer>'
app/serializers/person_serializer.rb:1:in '<top (required)>'
app/controllers/api/v1/people_controller.rb:21:in 'index'

jlehman avatar Mar 24 '16 10:03 jlehman

@jlehman per docs, method name in 0.10.x is cache

bf4 avatar Mar 25 '16 00:03 bf4