I have a model object that is not descended from ActiveRecord::Base and is not stored in the database. I created a serializer for it (with the same name + "Serializer"), and in my controller I'm calling render json: object_instance
.
The result is an exception from deep inside of render
.
I implemented an as_json
method that instantiates the serializer and calls it, but the result is then a missing method in the object, read_attribute_for_serialization
.
I want my object to act like an ActiveModel-compatible object at least as far as Active Model Serializer goes, but I don't see any reference to this in their documentation.
The model object needs to include it thusly:
# active_model_serializers 0.10+
class ModelName
include ActiveModel::Serialization
end
# active_model_serializers < 0.10
class ModelName
include ActiveModel::SerializerSupport
end
This implements the methods needed within the object, and also auto-discovers the serializer matching the object name, so it can be used transparently just like an ActiveRecord object.
This works with active_model_serializers version 0.8.1 through at least 0.9.3.
Following on from mutexkid answer, with active_model_serializers 0.10.0.rc4, the following is required to make a plain old ruby object play nice with a serializer:
PORO:
class SearchResult
include ActiveModel::Serialization
attr_reader :stories, :users, :friends
def initialize(search:)
@stories = search.stories
@users = search.users
@friends = search.friends
end
end
Serializer:
class SearchResultSerializer < ActiveModel::Serializer
attributes :stories, :users, :friends
end
I ran into this yesterday while upgrading to Rails 4.2. ActiveModel::SerializerSupport
has been removed for 0.10-stable. I ended up just adding an alias to my POROs which seemed to do the trick:
alias :read_attribute_for_serialization :send
(active_model_serializers 0.10.0.rc2)
You will also need to add include ActiveModel::Model
in your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With