When parsing a JSON API using ActiveModel::Serializers, is there a way not having to specify every single key in the JSON as attributes?
Say I only need :first_name, :last_name, :country
for my views -- unless I also specify the other keys in the JSON, :street_address, :postal_code, :phone, :email
, I will get undefined method 'street_address=' for #.
I found http://bigastronaut.com/blog/2014/don-t-serialize-just-a-few-give-me-all-attributes but his PR has not yet been accepted: https://github.com/rails-api/active_model_serializers/pull/535 -- is there something else I could do meanwhile?
class GetFromJson
include ActiveModel::Serializers::JSON
attr_accessor :first_name, :last_name, :country # :street_address, :postal_code, :phone, :email
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
def self.fetch
# Code to fetch JSON from API
end
end