对于[]:Array,Rails未定义方法`update_attributes'

时间:2011-02-21 00:17:35

标签: ruby-on-rails ruby ruby-on-rails-3

尝试更新属性时出错。

   C:\Rails\App>rake reklamer:iqmedier
    (in C:/Rails/App)
    rake aborted!
    undefined method `update_attributes' for []:Array
    C:/Rails/App/lib/tasks/statistik.rake:23:in `block (2 levels) in
     <top (required)>'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:634:in `call'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:634:in `block in execute'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:629:in `each'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:629:in `execute'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:595:in `block in invoke_with_call_chain'
    C:/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:588:in `invoke_with_call_chain'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:581:in `invoke'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2041:in `invoke_task'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:in `each'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
    C:/Ruby192/lib/ruby/1.9.1/rake.rb:1992:in `run'
    C:/Ruby192/bin/rake:31:in `<main>'

以下是我的佣金任务的一部分:

  @stats = agent.page.search('//tr')[-2].search('td').map{ |n| n.text }

  @existing = Reklamer.find(:all, :conditions => {:dato => @stats[0]})
  if @existing.nil?
      Reklamer.create!(:virksomhed => 'Iqmedier', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2], :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
  else
    @existing.update_attributes(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
  end

2 个答案:

答案 0 :(得分:4)

问题是你找到了所有,而且你无法更新数组,所以你有两个选择:

(1)更新所有匹配的Reklamer对象:

@existing = Reklamer.find(:all, :conditions => {:dato => @stats[0]})
  if @existing.empty?
      Reklamer.create!(:virksomhed => 'Iqmedier', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2], :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
  else
    @existing.each{ |e| e.update_attributes(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8]) }
  end

(2)仅更新Reklamer对象的单个实例:

@existing = Reklamer.find(:first, :conditions => {:dato => @stats[0]})
  if @existing.nil?
      Reklamer.create!(:virksomhed => 'Iqmedier', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2], :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
  else
    @existing.update_attributes(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
  end

答案 1 :(得分:0)

ActiveRecord的#find(:all, ...)返回记录数组,而不是单个记录。如果您想更新所有匹配记录,则可以@existing.each {|r| r.update_attributes(...)}。如果您想更新单个记录,则需要执行Reklamer.find(:first, ...)条件,以确保只匹配您希望更新的记录。