Rails Active Model Serializer — need to manipulate rendered json prior to
send
So here's a fun one — I have a controller method that returns a list of
"resumable" objects based on some rules. But, there are criteria i can't
include in a single database query — so, I'd like to render out the "base"
resumable list, and then essentially run a filter on it prior to returning
to the client.
In other words, what I'm trying to do is:
sessions = ended_sessions.where("current_date <= (created_at + interval
'12 hours')")
json = JSON.parse(SessionResumeSerializer.new(sessions).as_json)
json.each do |session|
# additional processing
end
Anyway, SessionResumeSerializer.new(sessions).as_json totally doesn't
work, throwing an :
NoMethodError: undefined method 'read_attribute_for_serialization' for
<ActiveRecord::Relation:0x007ffa164b0ce0> error.
I'm clearly confused about how this is supposed to work — seems completely
intuitive that the initializer would take a relation and render it out.
Is there a better way to go about this?
No comments:
Post a Comment