How do I combine :finder_sql and :conditions to perform a su

I’m sure there’s something right under my nose that I’m missing.

I have two tables with two parallel one-to-many relationships. I wish
to use the :finder_sql parameter to essentially ‘or’ the two foreign

What isn’t working for me is performing a ‘sub-search’.

Let’s say the tables are “stores” and “people,” and the relationships
are called “works_for” and “shops_at.”

I want to have a single “has_many” relationship that encompasses the
people who visit a store. Something like:

class Store < ActiveRecord::Base
has_many :people,
:dependent => true,
:finder_sql => ‘SELECT DISTINCT p.*
FROM people p
WHERE (p.works_for_id = #{id} OR
p.shops_at_id = #{id})’,
:order => “updated_on DESC”

Meanwhile people is an STI:

class Person < ActiveRecord::Base

class Employee < Person
belongs_to :store,
:foreign_key => ‘works_for_id’

class Shopper < Person
belongs_to :store,
:foreign_key => ‘shops_at_id’

This all seems to work for the common cases. For example, given an
instance of Store called macys, I can get its visitors with

However, this all goes badly when I try to perform a sub-search. What
I want are all of macy’s people that have been recently updated.

Something like:

macys.people.find_all :conditions => [‘updated_on >= ?’,

What I get is all of the people at macys. It seems to ignore the

I’ve tried :

macys.people.find :all, :conditions => [‘updated_on >= ?’,

And I get an exception inside of association_proxy.rb

Anyways, that’s very long-winded. If we snip it all off, what I want
is to be able to add conditions to a relationship that has its own

Is there something else I should be doing?

Thanks in advance!

Reginald Braithwaite
“Our show may not be fancy, but it’s noisy and it’s free.”

Like all text messages, email exchanged with a gmail account may be
stored indefinitely and/or read by third parties without the sender or
receiver’s knowledge or permission. Please do not send any privileged
or confidential transmission to this account: enquire about secure