# Help with calculation

**URL:** <https://www.ruby-forum.com/t/help-with-calculation/155851>\
**Category:** Rails\
**Created:** [December 17, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/help-with-calculation/155851 "2008-12-17T15:56:16Z")\
**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 17, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/help-with-calculation/155851/1 "2008-12-17T15:56:16Z")

</div>

I need some help with a calculation in a survey that I run.

I have one table with words, characteristics, such as “careful,  
dominant, positive” etc.

People are then to select the words they think belong to a visual  
detail, such as a square, circle, symmetry, etc. They can select 1-3  
words.

These are then stored in one string field, as a string with space  
between each word, in the table used for answers. So each visual detail  
gets its own row with this “word field”.

What is the best way to count how many that have selected one of the  
words for a particular visual detail, e.g how many have selected “hard”  
for a square?

I could collect the words in one find and then use count for each, but  
that will be many searches, so I figured I could do one search only in  
the answers table, and then loop through it and check how often the word  
is selected. But I’m not sure how to do that. I’m thinking of some  
functions in Array that I could use, or…?

Hopefully you understand the setup and my need. Any ideas?

---

<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 17, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/help-with-calculation/155851/2 "2008-12-17T16:07:50Z")

</div>

The information is a bit vague, so I can’t see the table names,  
but something like:

Model.count(:conditions =\> [“answer LIKE %?%”, searched\_word])

Add more conditions to select answers for squares only

Model.count(:conditions =\> [“answer LIKE %?% AND type=‘square’”,  
searched\_word])  
(however this type information is stored)

Depending on the actual structure of your models/database,  
additional includes may be necessary or you may have to  
fall back on plain SQL.

In any case I would try to keep it in SQL for performance reasons,  
not let Ruby array functions count through lots of records.

---

<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 17, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/help-with-calculation/155851/3 "2008-12-17T16:39:00Z")

</div>

On 17 Dec 2008, at 14:56, Pål Bergström wrote:

> These are then stored in one string field, as a string with space  
> between each word, in the table used for answers. So each visual  
> detail  
> gets its own row with this “word field”.
> 
> What is the best way to count how many that have selected one of the  
> words for a particular visual detail, e.g how many have selected  
> “hard”  
> for a square?

Sounds to me like it would be easier for answer to has  
+many :visual\_details (:through some appropriate join model) and  
similarly in the other direction.  
You might end up with it being as easy as  
some\_visual\_detail.words.count (though i suspect you’d end up needing  
nested has many through for that level of conciseness (and that’s not  
supported yet)

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 17, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/help-with-calculation/155851/4 "2008-12-17T18:22:36Z")

</div>

Thorsten M. wrote:

> The information is a bit vague, so I can’t see the table names,  
> but something like:
> 
> Model.count(:conditions =\> [“answer LIKE %?%”, searched\_word])
> 
> Add more conditions to select answers for squares only
> 
> Model.count(:conditions =\> [“answer LIKE %?% AND type=‘square’”,  
> searched\_word])  
> (however this type information is stored)
> 
> Depending on the actual structure of your models/database,  
> additional includes may be necessary or you may have to  
> fall back on plain SQL.
> 
> In any case I would try to keep it in SQL for performance reasons,  
> not let Ruby array functions count through lots of records.

Sorry for the vague info.

I thought of this. But isn’t that a burden on the server searching for  
each word of, let’s say 30, on several thousand answer? Or does it go  
fast enough?
