Desired loading with conditions

The newer impatient download style uses several queries to load associations. Can I add conditions for these additional downloadable download requests? eg.

Bakery.find(:all, :include => :bakers)

will generate the desired download request, similar to this:

SELECT bakers . * FROM bakers WHERE ( bakers .bakery_id IN (1,2,3,4,5))

Can I add conditions for this impatient download request?

Update: To (possibly) make this clearer, the query I want to replicate using AR (sans SQL) is:

SELECT * FROM bakeries LEFT JOIN bakers ON bakeries.id = bakers.bakery_id AND bakers.hat = 'on'

In particular, I was hoping I could modify the second loaded SQL statement:

SELECT bakers . * FROM bakers WHERE ( bakers .bakery_id IN (1,2,3,4,5) and bakers .hat = 'on')

Is this possible, or should I use SQL? Just looking for a "Rails" way to do this. :)

+4
source share
1 answer

Well, what exactly do you want to change in a related query?

You can of course do something like this:

 Bakery.find(:all, :include => :bakers, :conditions => ["bakers.something = ?", true]) 

If we had a little more information, there might be a better way to do this. Depending on what you are looking for, you may look into named_scope s, which you can combine, for example:

 Bakery.bakers.available 

named_scope pretty neat.

+1
source

All Articles