Search code examples
jsonruby-on-rails-4active-model-serializers

JSON.parse conflicting with ActiveModel::Serializers::JSON::Module?


I am parsing JSON returned from Google places in one of my models. It ran fine until I started implementing ActiveModel Serializers, and now I'm getting this error:

undefined method `parse' for ActiveModel::Serializers::JSON:Module

The even trickier part is that running each command through the command line still works fine. Here is the code where the error occurs. Is the ActiveModel Serializer library conflicting with the JSON library? I couldn't find anyone mentioning this issue, so maybe I'm just missing something basic. Any ideas would be greatly appreciated.

def self.create_from_google(ref, current_user_id, google_api_key)
  feed_url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=#{ref}&sensor=true&key=#{google_api_key}"
  resp = Net::HTTP.get_response(URI.parse(feed_url))
  data = resp.body
  json = JSON.parse(data)
  if json["status"] != "OK"
    return false
  elsif json["status"] == "OK"
    ...
  end
end

Solution

  • maybe try Object::JSON.parse(data). I think it's getting confused on which JSON module you are referencing