# Eager loading with dynamic conditions?

**URL:** <https://www.ruby-forum.com/t/eager-loading-with-dynamic-conditions/178152>\
**Category:** Rails\
**Created:** [October 29, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/eager-loading-with-dynamic-conditions/178152 "2009-10-29T22:46:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [October 29, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/eager-loading-with-dynamic-conditions/178152/1 "2009-10-29T22:46:51Z")

</div>

If I have an association

A habtm B

and I say A.find…, :include =\> B

but I only want some of the B’s to be loaded, the rails docs (http://  
[ar.rubyonrails.org/classes/ActiveRecord/Associations/](http://ar.rubyonrails.org/classes/ActiveRecord/Associations/)  
ClassMethods.html) say:

If you do want eagerload only some members of an association it is  
usually more natural to :include an association which has conditions  
defined on it:

class Post \< ActiveRecord::Base  
has\_many :approved\_comments, :class\_name =\> ‘Comment’, :conditions  
=\> [‘approved = ?’, true]  
end

Post.find(:all, :include =\> :approved\_comments)

Which is fine if you know ahead of time what the conditions are. If  
you only know the condition at the time of the request, things get  
trickier. For example, if B has a user\_id field, and you only want to  
show this user’s B’s.

One solution I came up with was to create a cattr\_accessor in A and  
set it upon login, and then use that in the association, as the docs  
suggest.

cattr :current\_user  
…  
has\_one :users\_b, :class\_name =\> ‘B’, :conditions =\> “user\_id = #  
{current\_user.id}”

This works fine, but it fails miserably when class caching is enabled  
(in production). And I’d much rather send user\_id as a parameter, and  
use that param in a find.

So, is there any way to have conditional eager loading with dynamic  
conditions?

Note, :conditions does not work because those conditions are applied  
to the entire join, not just the left joined table (this is verified  
in the docs, same link as above).

Thanks for any help,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [October 30, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/eager-loading-with-dynamic-conditions/178152/2 "2009-10-30T22:41:45Z")

</div>

In this case you should use 2 finds. One to find the records and one  
to find all associated records. On the associated records array you  
can do a find { |record| record.associated\_id == actual.id} this is  
the same effect as eager loading. I dont know if a lambda would work  
on the association, though.

2009/10/29, dino d. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> …  
> Note, :conditions does not work because those conditions are applied  
> to the entire join, not just the left joined table (this is verified  
> in the docs, same link as above).
> 
> Thanks for any help,  
> Dino
> 
> >

–  
Von meinen Mobilgerät aus gesendet
