I am trying to create a simple data puller from the yahoo finance gem. I have a nested route setup for Security that has_many Price
The Price controller is as follows:
class PricesController < ApplicationController
before_action :set_security
before_action :set_price, only: [:show, :edit, :update, :destroy]
def new
@price = Price.new
end
private
def set_price
@price = Price.find_by_name(params[:id])
end
def price_params
params.require(:price).permit(:date, :open, :high, :low, :close, :volume, :security_id)
end
def set_security
@security = Security.find_by_ticker(params[:security_id])
end
end
The nested route works fine to manually create the child Price record from a form. I'm trying to create a Price record for each result in the array that gets generated from the following code:
class Datapuller
def self.download_historical
yahoo_client = YahooFinance::Client.new
data = yahoo_client.historical_quotes("FB")
data.each do |i|
@ticker = Security.find_by_ticker(data[0].symbol )
price = @ticker.prices.new()
price.security_id = @ticker
price.date = data[0].date
price.open = data[0].open
price.high = data[0].high
price.low = data[0].low
price.close = data[0].close
end
end
end
I'd like to call this method from a button link but for now I have the following code on the Security View:
<%= Datapuller.download_historical %>
This also give me the benefit to see the data that is getting loaded from the array.
With the above code I can create a child Price to a parent Security using a form and I can make the call from the yahoo finance gem. However, the array is not creating a Price record for each line in the array.
What am I missing in this?
Thanks!