Forum: Ruby on Rails activerecord(mysql) and hash to unique count

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Berlin B. (Guest)
on 2006-04-18 10:03
(Received via mailing list)
I am working with ruby(activerecord) and want to save a 'hash' of URLs
such that a key is the URL and the value points to a count of how many
times it occurs after processing a csv file
my question; all I know of is right now is to attempt to save, get a
unique error and then when handling the exception increment the hash
value (really a column in a table)

Is this bad?  Or are there better ways?

  begin
      word = NumbersWord.new
      word.word = w.strip
      word.source = url_source
      word.save
  rescue

      puts "ERR: Could not save system word (possible unique error)"
      # Potentially, we need to update the record
      found = NumbersWord.find(:first, :conditions => [ "word = ?", w ],
:limit => 1)
      if found
        found.occurrence = found.occurrence + 1
        found.save
      end

      # End of Exception
  end

--
Berlin B.
(ramaza3 on freenode)
http://www.newspiritcompany.com
http://www.newspiritcompany.com/newforums
also checkout alpha version of botverse:
http://www.newspiritcompany.com:8086/universe_home
This topic is locked and can not be replied to.