I have a rails app with 2 models namely a security and a stock_quote model
security.rb
has_many :stock_quotes, dependent: :destroy
stock_quotes.rb
belongs_to :security, class_name: "Security", foreign_key: 'security_id'
I have used rabl to create a JSON API to display a json of securities and their respective stock_quotes. The format is as follows:
security/show.html.rabl
object @security
attributes :id, :security, :security_code, :category
node(:edit_url){ |security| edit_security_url(security)}
child :stock_quotes do
attributes :stock_quote_id, :security_id, :yesterday, :current, :price_change, :percentage_change, :high, :low, :published_at
end
security/index.html.erb
collection @securities
extends "securities/show"
The API was successfully created and when I visit /securities.json I get the data that is in the database in the following format
[
{
"security": {
"id": 1,
"security": "Google",
"security_code": null,
"category": null,
"stock_quotes": [
{
"stock_quote": {
"security_id": 1,
"yesterday": 8.6,
"current": 9.45,
"price_change": 0.85,
"percentage_change": 9.88,
"high": 9.45,
"low": 9.45,
"published_at": "2014-05-29T08:04:54.000Z"
}
}
]
}
},
{
"security": {
"id": 2,
"security": "Facebook",
"security_code": null,
"category": null,
"stock_quotes": [
{
"stock_quote": {
"security_id": 2,
"yesterday": 30,
"current": 31.75,
"price_change": 1.75,
"percentage_change": 5.83,
"high": 31.75,
"low": 30.75,
"published_at": "2014-05-29T08:04:54.000Z"
}
}
]
}
}
]
I however cannot work with this data. I have even tried to log it to the console to no avail and it returns no error on the firebug console.Here is a simple script in my securities/index.html.erb file that I'm using to log the data to the console.
<div id="sec">
<script type="text/javascript">
var url = "/securities.json";
var stock = {};
$.ajax({
url: url,
cache: false,
dataType: 'jsonp', //will set cache to false by default
context: stock,
success: function(data){
console.log(data);
}
});
</script>
</div>
After visiting the /securities page, and checking my firebug console, I see that a get request to /securities.json has been performed but however my data is not being logged to the console as I have stated in my code.
How do I get this data to get logged to the console, since I'm sure that if I am able to log it on the console,using that data for charting which is what I intend to, will not be a problem.