轨道试图从一个子表更新父表时4.1.0没有方法错误 [英] Rails 4.1.0 No Method error when trying to update a parent table from a child table

查看:117
本文介绍了轨道试图从一个子表更新父表时4.1.0没有方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个导轨型号,安全性和stock_quote模型,如下:

I have 2 rails models, a security and stock_quote model, which are as follows

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>

我haev现在提供的完整数据集和模型的外观stock_quote.rb的我用feedzija宝石获取andparse的RSS

I haev now provided the full data set and model look of stock_quote.rb I'm using feedzija gem to fetch andparse the rss

推荐答案

有没有这样的方法,属于父模型子模型。只有的has_many或HAS_ONE有这样的方法产生,或建立。但是,有使用这种模式其他方法的打造_ 创建_ belongs_to的的关联。

There is no such method for the child model which belongs to the parent model. Only has_many or has_one has such methods create, or build. But, there is some other method which uses this pattern build_ or create_ for belongs_to association.

所以,如果你不喜欢这样,

So if you do it like this,

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

这将正常工作。

It will work fine.

这篇关于轨道试图从一个子表更新父表时4.1.0没有方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆