Rails 4.1.0 No Method error when trying to update a parent table from a child table

旧时模样 提交于 2019-12-08 13:57:50

问题


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 

When I go to the console and do

c = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values"
StockQuote.update_from_feed(c)

I get this

(0.1ms)  SELECT COUNT(*) FROM "securities"  WHERE "securities"."security" = 'Sameer africa'
NoMethodError: undefined method `create_security' for #<Class:0xbf84f98>

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


回答1:


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.




回答2:


Create is a class method but you are using it in an instance of a security

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

In the next part #create_security is used as a class method it has to be called on the instance of a StackQuote



来源:https://stackoverflow.com/questions/23537226/rails-4-1-0-no-method-error-when-trying-to-update-a-parent-table-from-a-child-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!