grape-entity
grape-entity copied to clipboard
Representations cannot be serialized/cached
Hey guys! I'm having an issue using Grape with cache (Garner) and send the results with grape-entity.
The issue is: the second time I request an object Garner start working, but the call for Grape Entity have a problem, because it sets the options as nil, making grape-entity to throw an error, options is not define.
Versions Grape 0.6.1 Grape-entity 0.4.0 Garner 0.3.2
Can you show some code please? What options?
well the options variable which is nil is present on the line https://github.com/intridea/grape-entity/blob/master/lib/grape_entity/entity.rb#L394 and actually the error is because the merge can't be done to a nil object
the controller looks like this
route(:get, ':id') do
cache(bind: { klass: ClassName, object: { id: params[:id] } }) do
# Some validations
object = Object.find_by_id(params[:id].to_i)
Object.represent(object, :uri=>"some_uri")
end
end
I see. I think the real bug is that a representation cannot be cached (serialized/deserialized). It would be helpful if you could build a failing test within grape-entity's RSpec tests that describes that.
As a workaround you could serialize the JSON if that's your only format, for example, which is probably better anyway for performance, so:
route(:get, ':id') do
cache(bind: { klass: ClassName, object: { id: params[:id] } }) do
# Some validations
object = Object.find_by_id(params[:id].to_i)
Object.represent(object, :uri=>"some_uri").as_json
end
end
It's a tradition on the team to give an ice cream for a fix like this, so we own you!! haha We'll use for now the workaround, but I'll make the test for this scenario in my free time.
Thank you very much.
So that .as_json
worked, right?
kind of late, but yes it does worked