On October 6 Michael Satterwhite asked the following question:
"When the generate script creates a model, it creates a subclass of
ActiveRecord. Is this to say that every table - even pure support tables
such as a table for state codes - should have a model?"
Mario Cicio responded with:
"No, on the contrary this is to say that every model is supposed to have
a persistency attached (simple case: 1 table).
The most common case of a table not having a model attached is the "join
table" in a habtm relationship."
This created a bit of confusion for me and I'm not sure whether I just read thing wrong or what. So I thought it was time to reveal my ignorance by asking for a bit of clarification.
Let's say I have a support table, say a list of states the user can choose from. If I do not have a "State" model, how do I access the table in my controller. Without the model, I can't do State.find :first, :conditions => X, can I? How, then do I look up a state in the table? Am I just way off here, or what?
I guess I'm asking is what is the best way to handle support tables like states, cities, appointment types, etc.?
If you don't want the overhead of a model for certain data, you can
always just make direct queries on the database.
For the cases you mentioned, though, I would probably just do the easy
thing and make them into models. Models aren't just for the major
things.
Those cases also differ from the HABTM (Has and belongs to many)
support table. That table's sole purpose is to join to things together.
It's not really a "thing" itself because it doesn't have any of its own
attributes.
If you don't want the overhead of a model for certain data, you can
always just make direct queries on the database.
I guess this is where my question is. How would I directly query a database in rails? I've always used a model and used Model.find.
For the cases you mentioned, though, I would probably just do the easy
thing and make them into models. Models aren't just for the major
things.
I'm with you here. I've always used a model for these kinds of tables. I was just thrown a bit by the thought of doing it differently. My curiosity was piqued. Hate it when that happens.
Let's say I have a support table, say a list of states the user can
choose from. If I do not have a "State" model, how do I access the
table in my controller. Without the model, I can't do
State.find :first, :conditions => X, can I? How, then do I look up a
state in the table? Am I just way off here, or what?
I guess I'm asking is what is the best way to handle support tables
like states, cities, appointment types, etc.?
This is handled pretty well by virtual enumerations: