Setting dynamic variables

Yikes! Not like that; what if one of the words is "thewordlist" :wink:

Word.find(:all) will return something that acts like an Array

@word_counts = {}
Word.find(:all).each do |w|
   @word_counts[w.word] = Publicsurvey.count(:conditions => ["word LIKE ?", w.word])
end

Then you can process the @word_counts hash however you like.

It seems like you could push the work off to the database with something like the following if the words don't have any wildcards in them that would cause LIKE to give a result different that just =.

sql = <<-END
   SELECT words.word, count(publicsurveys.id) as 'count'
   FROM words LEFT OUTER JOIN publicsurveys ON publicsurvey.word = words.word
   GROUP BY words.word
END

@word_counts = {}
Word.connection.select_all(sql).each {|rowhash| @word_counts[rowhash['word']] = rowhash['count']}

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com