I have a class in Ruby (on Rails) as follows:
class Good
include MongoMapper::Document
has_many :offers, :dependent => :destroy
key :name, String
key :description, String
key :email, String
key :offers, Array
key :type, String
end
I am parsing a JSON object from an external URL and retrieve only a partial set of the types in this class, ie, I can retrieve only name, description, and type. On my server, I already have a page and data that can be displayed. I would like to display the data from the external URL on the page as well. How can I do this without making too many changes to the code? One thing I thought of was to make the strings I retrieve a part of the class Good, but I have no idea of how I can do it.
In other words, the retrieved name, description and type will be cast into the type Good and then will use the existing page, created to display our goods to display both, our, and the retrieved goods. How can I do this? Is this the right way of going about it?
Your question is pretty unclear, but I’ll take a shot. You could have a
StructlikeYou could then map a collection of
Goodinstances to thisThen you can gather your JSON response and append new
ArbitraryGoodinstances to it.Now
@arbitrary_goodscontains a single listing mixed with data from bothGoodinstances and the parsed external JSON for use within your view.