I forgot include ActiveModel::Serializers::JSON
module and attributes
method. After this fix all work as expected:
class Foo
include ActiveModel::Model
include ActiveModel::Serializers::JSON
attr_accessor :a, :b
def attributes
{'a' => nil, 'b' => nil}
end
end
Foo.new(a: 1, b: 2).as_json
# {"a" => 1, "b" => 2}
Foo.new(a: 1, b: 2).as_json(only: [:a])
# {"a" => 1}