【发布时间】:2026-02-17 06:25:01
【问题描述】:
我有 2 个 rails 模型,一个 security 和 stock_quote 模型,如下
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 "not an array\n"
end
end
private
def self.add_nonexistent_security(entry)
a = StockQuote.create_security(security: entry.title.capitalize, category: "Uncategorized")
end
def self.save_entry(entry,security_id)
unless exists? guid: entry.id
contents = entry.content.split(',').map {|n| n.split(" ").last }
parsed_contents = contents.map{ |x| x.scan(/[\d\.-]+/)[0] }.map(&:to_f)
parsed_contents.each do |content|
create!(
security_id: b.id,
yesterday: content[0],
current: content[1],
change: content[2],
percentage_change: content[3],
high: content[4],
low: content[5],
published_at: entry.updated,
guid: entry.id
)
end
end
end
class Security < ActiveRecord::Base
has_many :stock_quotes, dependent: :destroy
end
当我去控制台做的时候
c = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"
StockQuote.update_from_feed(c)
我明白了
(0.1ms) SELECT COUNT(*) FROM "securities" WHERE "securities"."security" = 'Sameer africa'
NoMethodError: undefined method `create_security' for #<Class:0xbf84f98>
我现在提供了 stock_quote.rb 的完整数据集和模型外观,我正在使用 feedzija gem 来获取和解析 rss
【问题讨论】:
-
@Baloo 这是另一个问题。
-
编辑之前没有
-
你有一个错字。你的意思可能是
a = self.create_security(security: entry.title.capitalize, category: "Uncategorized")
标签: ruby-on-rails activerecord ruby-on-rails-4 associations