Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Pass variable from Controller to Serializer in Rails

Rails • Asked by Rony Fhebrian

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.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

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

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.