# Filtering find by values in associated models?

**URL:** <https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263>\
**Category:** Rails\
**Created:** [April 9, 2008, 11:43am UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263 "2008-04-09T11:43:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [April 9, 2008, 11:43am UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263/1 "2008-04-09T11:43:43Z")

</div>

Given

class Customer  
belongs\_to :invoice\_address, :class\_name =\> “Address”, …  
belongs\_to :delivery\_address, :class\_name =\> “Address”, …  
belongs\_to :contact, :class\_name =\> “Employee”, …  
end

How would you go about filtering a find :all where e.g.  
invoice\_address.city = “Oslo”, delivery\_address.zip = 1234 and  
contact.department = 14?  
Exactly which and how many fields we want to filter on is highly  
dynamic. Doing it in the db is a must.

Suggestions?

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [April 9, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263/2 "2008-04-09T15:19:42Z")

</div>

eager loading of associations should get in the right direction:

Customer.find(:all, :include =\> [:invoice\_address, :delivery\_address,  
:contact], :conditions =\> [“Address.city=? AND Address.zip=? AND  
contact.department=?”, “Oslo”, 1234, 14])

but this example wouldn’t do exactly what you want, since it’s mixing  
invoice & delivery address.  
it will take some work, to set this up the right way, maybe get a  
working sql solution first.

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [April 10, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263/3 "2008-04-10T13:00:49Z")

</div>

On Wed, Apr 9, 2008 at 3:19 PM, Thorsten M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> working sql solution first.  
> We’re currently thinking of feeding :joins an sql fragment along the  
> lines of:  
> conditions.keys.substring(“.”) do |alias|  
> “inner join \<alias\_to\_table\_name\> as on (.id =  
> customer.\_id)”  
> end.join(" ")

Which would use a condition hash like:  
{“invoice\_address.city” =\> “Oslo”, “delivery\_address.zip” =\> “1234”,  
…}

Unless someone can come up with a better approach.

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [April 11, 2008, 1:41am UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263/4 "2008-04-11T01:41:59Z")

</div>

I must be missing something.

Why can’t you simply eager-load the associations and do your  
“invoice\_address.city” =\> “Oslo” type conditions?

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [April 11, 2008, 9:22am UTC](https://www.ruby-forum.com/t/filtering-find-by-values-in-associated-models/134263/5 "2008-04-11T09:22:39Z")

</div>

On 11 Apr 2008, at 00:41, Julian L. wrote:

> I must be missing something.
> 
> Why can’t you simply eager-load the associations and do your  
> “invoice\_address.city” =\> “Oslo” type conditions?

Because the table addresses is joined twice you’d need to know what it  
was aliased as.

Fred
