It is interesting to think about traditional db “lookup” tables in
rails. Consider the notion of a “stage.” A stage can be “active”,
“suspended”, “completed”, “terminated”, etc. In rails thinking I would
have some object models which happen to be “stageable” (polymorphic
associations). Example: an employee could be stageable (employee could
have many stages- each stage has a stage type). Obviously, the
“stage_type” model is the traditional lookup table.
Maybe I lack proper understanding but I tend to fear the “find(:all…)”
method because I am thinking in terms of thousands of rows. I want to
return a list of objects which have some “stage” in the lookup table
(with some other conditions). But why do I have to load the whole
collection?
I mean it seams like I am forced to load that whole collection and then
do operations on the collection to group them by stage type (the lookup
table). Then I would have to extract the grouped subset.
Is there not a better way? I am trying to think about it without
resorting to a new join model (since I don’t know how I would
parameterize the join model for the different values in the lookup
table).
Can anyone help straighten my wayward thinking?
Thanks,
If I was doing this I would just have a table stage and a :belongs_to
for each object that can have stages. Then you can query any table on
its stage or set of stages in a single query. If you are wanting all
objects of any type in a stage then things get very messy as you
indicated because you are navigating from the stage using a
polymorphic association. In general though that seems overkill.
Generally I want things grouped or filtered by stage not all objects
regardless of type in a stage.
Michael
On May 28, 1:09 pm, Philip D. [email protected]
Thanks, MichaelLatta, for your input. I still think I am not
understanding Rails filtering and finders at a very deep level. Yes it
is easy to filter simple models using the built in finder methods. When
using these methods with simple tables, I feel like I am working in the
object oriented methodology. However, when there are more objects (like
employees and stages) I feel like the finders and filtering methods do
loose their object oriented feeling. Why? They do not provide access
to anything but the one model you started with. As soon as I decide to
use the find methods for the Employee object then I immediately have no
search level access to the “other” objects owned by Employee instances.
Thanks for being patient. I simply want to understand how to do
filtering and “finding” by using object oriented “style” only (and not
using SQL and also without the complete table being loaded into a
collection).