Best way to see if an item is in a list

Hey there, i am in my 4th day of working in rails. I dig it a lot. I
have a question about the best way to go about something.

We monitor the conditions of equipment
each piece of equipment has a current status.
each status will corrospond to a list in a table that we have called
key_words
key_words is just kinda like a configuration settings table, no
relationships, just a list of keys and values.

so i have something like this in key_words
key = ‘running’ value = ‘r, run, running, rng, pwr, power_on’
key = ‘not_running’ value = ‘s, stp, stop, not_rng’

so each value is actually a list of possibilities. i need to be able to
pass a status to see which list its in.
so if i pass ‘r’ to the function, it will return ‘running’ and i can
use that to display an image on the website that shows that the machine
is running.

now, i am pretty sure i can pull this off, but i wanted some opinion of
where to put it. Do i put it in the key_words.rb model,
key_words_controller.rb or in the application.rb ( as it will be used
in more than one place)

i thought it would go a little something like this

def check_if_running(status)
running = KeyWord.find(1, :conditions => " key = ‘running’ " ) #
this will get me the list
look thru pickaxe to see how to tell if status in the list returned
return true if (status is in running list)
return false
end

i suppose it would look different if its in the key_words.rb model

any advice ?

On 11/10/06, nephish [email protected] wrote:

relationships, just a list of keys and values.

look thru pickaxe to see how to tell if status in the list returned
return true if (status is in running list)
return false

end

i suppose it would look different if its in the key_words.rb model

any advice ?

If the list of words is fairly static, the database may be overkill for
this

application - all you are really looking for is a mapping from status
words
to running/not-running. Have you considered implementing this as a
straight
hash table? If you’re concerned about maintaining the configuration, you
could pull the actual values out to a YAML file, and load it in
environment.rb.

For instance, add this to your environment.rb:

key_word_is_running = YAML::load_file(‘key_words.yaml’)

key_words.yaml looks like this:
r: 1
run: 1
running: 1
rng: 1
pwr: 1
power_on: 1
s: 0
stp: 0
stop: 0
not_rng: 0

In your code, you would simply say:

key_word_is_running[status]

Further refinement of the YAML format is certainly possible - for
example,
to
look more like the data in the original example (two lists, “running”
and
“not_running”).

If the database is a requirement, it still seems more efficient to map
the
values directly, rather than having unstructured lists in the values.
So your schema would be more like:

id (integer)
status_word (string)
running (boolean)

Then your code reduces to:

KeyWord.find_by_status_word(status).is_running?

Hope this helps,

Matt J.
[email protected]
President/Technical Director, Acme Art Company (acmeartco.org)