Querying in O/R Mappers
I’m confused about performing queries with O/R Mappers, when properties
don’t map directly to database fields.
* calculated fields ( eg def get_age(as_of = now) ; as_of - birthday ;
end -> method returns result of calculation, not simple accessor)
* different subclasses implement field differently (some may store it
directly, some may calculate it)
* Methods like def zip_code ; address.zip_code; end - which access the
fields of a member object (demeter friendly)
Let’s say I need to get all people whose age as of today is >18, or all
people whose zipcode is 01234.
I can think of two ways:
A) Retrieve all the objects, and run the method on each one.
B) Write special SQL to do the query.
The obvious downside to A is performance.
The obvious downside to B is that it ruins all the benefits of OO
encapsulation, demeter, inheritance, etc. I need to incorporate all of
that knoweledge into the finder methods. When different subclasses
implement the field differently, this gets into some pretty messy SQL as
How do the ActiveRecord pro’s do it?