Lets say I am having a Model called Vendor(id, name, lat, lon, created_at). And i have a query in which i find distance of vendors from the current latitude and longitude.
Query is -
query = "*, ST_Distance(ST_SetSRID(ST_Point(#{lat}, #{lon}), 4326)::geography,ST_SetSRID(ST_Point(lat, lon), 4326)::geography) as distance"
Vendor.select(query)
I have serializer class as -
class VendorSerializer < ActiveModel::Serializer
attributes :id,
:lat,
:lon,
:name,
:created_at
def attributes
hash = super
# I tried to override attributes and added distance but it doesn't add
hash[:distance] = object.distance if object.distance.present?
hash
end
end
I want to have a serialised object as {id, lat, lon, name, created_at, distance}. So the model attributes get attached but how can i add the extra field/attribute i.e, "distance" to the serialised hash?