1

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/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>'

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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

How do I prevent duplicates. Example one column time is: 2011-02-15 23:53:28 and it creates a new with time:2011-02-15 00:00:00. How to prevent that?
That makes sense. Those are different times, so comparing them will not result in equality. That's different from the issue you have here though, so I would recommend creating a new question thread with details surrounding what you are trying to accomplish. Don't forget to mark this question as accepted though :)
I dont understand why it is creating a 100% duplicate now? The time is the same I thougt I was checking for a duplicate
The two times you posted are different '2011-02-15 23:53:28' is not equivalent to '2011-02-15 00:00:00'. Although they are on the same day, they are 23 hours 53 minutes and 28 second apart. That's almost an entire day difference.
I have tested now and it is creating a 100% replica. The times are 2011-02-15 00:00:00 and 2011-02-15 00:00:00
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.