Reputation: 47
I have an API method that returns JSON floating-point identifiers (3971.0). Although in fact an integer (3971) should be output. How can this be remedied?
def index
@users = User.all
render json: @users
end
Upvotes: 0
Views: 30
Reputation: 2347
If you still want to go with solution using ActiveModel::Serializer, then you can do it as
gem 'active_model_serializers'
rails g serializer user
user_serializer.rb
under app/serializer
attributes :user_id, :login, :name, :spr_work_id
Let's say you want to modify the value of :spr_work_id to make it an integer then you have to define a method inside the serializer as so
def spr_work_id
self.object.spr_work_id.to_i
end
In your controller replace render json: @users
to @users.map{|user| UserSerializer.new(user).serializable_hash}
Upvotes: 1