Pass variable from Controller to Serializer in Rails
I have this code in my Controller to get distance in meters from one location to another location:
range = Geocoder::Calculations.distance_between([lat,lng],[Model.location.lat,Model.location.lng])
rangeInMeters = range * 1000
rangeRounded = rangeInMeters.round
rangeRounded = rangeRounded / 100 * 100
and I have this code in my serializer to give a nicer output for the json response like this:
class ModelSerializer < ActiveModel::Serializer
attributes :id, :name, :address, :range
def range
// something
end
end
How to get rangeRounded be read by ModelSerializer so in json response it would be like this:
{
"id": 1,
"name": "Name A",
"address": "Address A",
"range": 500
},