【问题标题】:Rails 4.1.0 No Method error when trying to update a parent table from a child table尝试从子表更新父表时,Rails 4.1.0 没有方法错误
【发布时间】: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


【解决方案1】:

对于属于父模型的子模型没有这样的方法。只有 has_many 或 has_one 有这样的方法 create 或 build。但是,还有一些其他方法使用这种模式 build_create_ 进行 belongs_to 关联。

所以如果你这样做,

b.create_security(security: "Facebook", category: "Tech")

它会正常工作的。

【讨论】:

  • 我已经发布了我的完整模型,你可以看到我已经尝试过了。检查编辑
  • 如何在课堂上调用 create_security 方法。它不是类方法。它是实例方法。首先获取股票报价。 stockQuote = StockQuote.find(1) stockQuote.create_security(属性)
【解决方案2】:

Create 是一个类方法,但您在安全实例中使用它

b.security.create(security: "Facebook", category: "Tech")

在下一部分中,#create_security 用作类方法,它必须在 StackQuote 的实例上调用

【讨论】:

  • 这不起作用,而是在控制台上返回它(0.1ms) SELECT COUNT(*) FROM "securities" WHERE "securities"."security" = 'Sameer africa' (0.1ms) SAVEPOINT active_record_1 SQL (0.5ms) INSERT INTO "stock_quotes" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-05-08 09:17:23.891679"], ["updated_at", "2014-05-08 09:17:23.891679"]]
最近更新 更多