# Setting dynamic variables

**URL:** <https://www.ruby-forum.com/t/setting-dynamic-variables/155985>\
**Category:** Rails\
**Created:** [December 18, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/setting-dynamic-variables/155985 "2008-12-18T22:40:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 18, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/setting-dynamic-variables/155985/1 "2008-12-18T22:40:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 18, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/setting-dynamic-variables/155985/2 "2008-12-18T22:58:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 18, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/setting-dynamic-variables/155985/3 "2008-12-18T23:04:06Z")

</div>

On Dec 18, 2008, at 4:40 PM, Pål Bergström wrote:

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [December 18, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/setting-dynamic-variables/155985/4 "2008-12-18T23:10:06Z")

</div>

Rob B. 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 🙂
