【问题标题】:Populate a model in a has_many relationship在 has_many 关系中填充模型
【发布时间】:2014-05-13 09:45:47
【问题描述】:

我有一个从以下 rss_feed 获取数据的 rails 应用程序

a="https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"

我正在使用 feedjira 从提要中获取和解析数据。我有 2 个模型,security.rb 和 stock_quote.rb 模型如下:

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

但是,当我转到我的应用程序的 rails 控制台并执行时

 z = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"

 StockQuote.update_from_feed(z)

我收到一个错误:

 NoMethodError: undefined method `stockquote' for #<String:0xcaf76a0>

我也试过

 d.stockquotes.create!

但我得到同样的错误

 NoMethodError: undefined method `stockquotes' for #<Security:0xb786e7c>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 associations has-many


    【解决方案1】:

    你试过了吗?

    d.stock_quotes.create!

    您的关系定义了has_many :stock_quotes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多