How do I set variables dynamically based on a search in the database? I have a list of 30 words in a table called ''words'' with a field called...words. I would like to loop through them and for each word set a variable. But I''m not sure how to do that. I''ve tried this but get an error: @thewordlist = Word.find(:all) if @thewordlist @thewordlist.each do |w| "@" + w.word = Publicsurvey.count(:conditions => ["word LIKE ?", w.word]) end end How should I do this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 18 Dec 2008, at 21:40, Pål Bergström wrote:> > How do I set variables dynamically based on a search in the > database? I > have a list of 30 words in a table called ''words'' with a field > called...words. I would like to loop through them and for each word > set > a variable. But I''m not sure how to do that. I''ve tried this but get > an > error: >Well there''s instance_variable_set, but this doesn''t sound like a good idea. What if the work isn''t a legal variable name (eg if it has a hyphen). What if the word is the same as some pre-existing instance variable ? This also makes it hard to iterate over all the instance variables set in this way. You''d be far better off bunging all that data in a hash Fred> @thewordlist = Word.find(:all) > > if @thewordlist > > > @thewordlist.each do |w| > > "@" + w.word = Publicsurvey.count(:conditions => ["word LIKE > ?", w.word]) > > end > > end > > How should I do this? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 18, 2008, at 4:40 PM, Pål Bergström wrote:> How do I set variables dynamically based on a search in the > database? I > have a list of 30 words in a table called ''words'' with a field > called...words. I would like to loop through them and for each word > set > a variable. But I''m not sure how to do that. I''ve tried this but get > an > error: > > @thewordlist = Word.find(:all) > if @thewordlist > @thewordlist.each do |w| > "@" + w.word = Publicsurvey.count(:conditions => ["word > LIKE ?", w.word]) > end > end > > How should I do this?Yikes! Not like that; what if one of the words is "thewordlist" ;-) 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On Dec 18, 2008, at 4:40 PM, P�l Bergstr�m wrote:> > Then you can process the @word_counts hash however you like. > > 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'']} >Perfect :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---