Help with calculation

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?

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.

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

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?