# Counting words for visual details

**URL:** <https://www.ruby-forum.com/t/counting-words-for-visual-details/176265>\
**Category:** Rails\
**Created:** [September 28, 2009, 8:49am UTC](https://www.ruby-forum.com/t/counting-words-for-visual-details/176265 "2009-09-28T08:49:15Z")\
**Posts on this page:** 3\
**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:** [September 28, 2009, 8:49am UTC](https://www.ruby-forum.com/t/counting-words-for-visual-details/176265/1 "2009-09-28T08:49:15Z")

</div>

I’ve made a survey in Rails where I want people to pick three words,  
characteristics for different personality types, from a list of 30 words  
for 20 different visual  
details such as linear, organic, symmetry, etc.

I save each answer for each user, with the visual detail in one column  
and the three words in an array in another column. E.g. for  
Linear a user doing the survey might pick Word1,Word2,Word3.

What would be the best way to count Word1 picked for let’s say  
Linearity? That is counting the occurrence.

One way would be:

Survey.count(:conditions =\> “details = ‘linear’ AND word like ‘Word1%’”)

But doing that for each of the 20 visual details and for each of the 30  
words would mean a lot of queries. I suspect the answer is somehow to do  
one search and then fix it with an array or hash method.

One last thing. My aim is to do something similar to tags that people  
use on their blogs for most frequent key words which also is bigger in  
font-size, if you know what I mean.

---

<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:** [September 28, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/counting-words-for-visual-details/176265/2 "2009-09-28T17:42:18Z")

</div>

[http://www.ibrahim-ahmed.com/2008/03/tag-clouds-in-ruby-and-rails.html](http://www.ibrahim-ahmed.com/2008/03/tag-clouds-in-ruby-and-rails.html) ?

What you are looking for is “tag clouds”. What I gave you is but one  
search result, YMMV.

---

<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:** [September 28, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/counting-words-for-visual-details/176265/3 "2009-09-28T19:35:26Z")

</div>

On Sep 28, 2:49 am, Pål Bergström [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Linearity? That is counting the occurrence.
> 
> One way would be:
> 
> Survey.count(:conditions =\> “details = ‘linear’ AND word like ‘Word1%’”)
> 
> But doing that for each of the 20 visual details and for each of the 30  
> words would mean a lot of queries. I suspect the answer is somehow to do  
> one search and then fix it with an array or hash method.

I think you’d be helped more by having a database schema that matches  
what you want to do better. Something like this sketch:

Detail: (id, name, position) acts\_as\_list, if you need the words to  
come up in order

DescriptiveWord: (id, name, position) [similar to above]

SurveyResponse: (id, user\_id, detail\_id, descriptive\_word\_id)  
(note that your app logic will need to constrain the user to 3 words)

All the usual associations can be set up based on this, and things  
like @detail.survey\_reponses.count(:group =\> ‘descriptive\_word\_id’)  
[counts responses to a particular @detail and returns a hash of  
word\_id =\> count; you can get the names with some clever :join usage]  
will work out of the box.

–Matt J.
