开发者

Rails undefined method `update_attributes' for []:Array

开发者 https://www.devze.com 2023-02-12 10:15 出处:网络
I get a error when trying to update atributtes. C:\\Rails\\App>rake reklamer:iqmedier (in C:/Rails/App)

I get a error when trying to update atributtes.

   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/rub开发者_开发百科y/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>'

Here is some part of my rake task:

  @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


The problem is you are finding all, and you can't update an array, so you have two options:

(1) Update all the matching Reklamer objects:

@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) Update only a single instance of the Reklamer object:

@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


ActiveRecord's #find(:all, ...) returns an array of records, not a single record. If you wanted to update all of the matching records, you would do @existing.each {|r| r.update_attributes(...)}. If you wanted to update a single record, you would need to do Reklamer.find(:first, ...) with conditions that ensured only the record that you desired to update would be matched.

0

精彩评论

暂无评论...
验证码 换一张
取 消