Reputation: 29
I have a rails app that fetches data from the following rss_feed
a="https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"
I'm using feedjira to fetch and parse data from the feed. I have 2 models, security.rb and stock_quote.rb model which are as follows:
security.rb
# == Schema Information
#
# Table name: securities
#
# id :integer not null, primary key
# security :string(255)
# category :string(255)
# created_at :datetime
# updated_at :datetime
#
class Security < ActiveRecord::Base
has_many :stock_quotes, dependent: :destroy
end
stock_quote.rb
# == Schema Information
#
# Table name: stock_quotes
#
# id :integer not null, primary key
# security_id :integer
# guid :string(255)
# created_at :datetime
# updated_at :datetime
# yesterday :decimal(8, 2)
# current :decimal(8, 2)
# price_change :string(255)
# percentage_change :string(255)
# high :decimal(8, 2)
# low :decimal(8, 2)
# guid_id :string(255)
# published_at :datetime
#
class StockQuote < ActiveRecord::Base
belongs_to :security, class_name: "Security", foreign_key: 'security_id'
def self.price_on(date)
where("date(created_at) = ?", date).sum(:high)
end
feed_url = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"
def self.update_from_feed(feed_url)
feed = Feedjira::Feed.fetch_and_parse(feed_url)
unless feed.is_a?(Fixnum)
add_entries(feed.entries)
else
puts feed.inspect
end
end
def self.update_from_feed_continuously(feed_url,delay_interval=2.minutes)
feed = Feedjira::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries)
loop do
sleep delay_interval
feed = Feedjira::Feed.update(feed_url)
add_entries(feed.new_entries) if feed.updated?
end
end
private
def self.add_entries(entries)
entries.each do | entry |
unless exists? guid: entry.id
b = entry.content
anydate = b.scan(/\d{4}\/\d{2}\/\d{2}/)
if !anydate.empty?
anydate.each {|s| b = b.gsub(s,'0').gsub(",",".")}
else
b = b.gsub(",",".")
end
content = b.scan(/(?<=[ *])-?\d[\d.]+/).map(&:to_f)
d=Security.find_or_create_by(security: entry.title.capitalize )
d.stockquote.create!(
yesterday: content[0],
current: content[1],
change: content[2].to_s,
percentage_change: content[3].to_s,
high: content[4],
low: content[5],
published_at: entry.updated,
guid: entry.id
);
end
end
end
end
However when I go to my app's rails console and do
z = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"
StockQuote.update_from_feed(z)
I get an error:
NoMethodError: undefined method `stockquote' for #<String:0xcaf76a0>
I have also tried using
d.stockquotes.create!
But i get the same error
NoMethodError: undefined method `stockquotes' for #<Security:0xb786e7c>
Upvotes: 0
Views: 403
Reputation: 1568
Have you tried this.
d.stock_quotes.create!
Your relationship defines has_many :stock_quotes
Upvotes: 2