# An Association Problem: AR::Base.find{:include} and n-deep a

**URL:** <https://www.ruby-forum.com/t/an-association-problem-ar-base-find-include-and-n-deep-a/54949>\
**Category:** Rails\
**Created:** [March 15, 2006, 1:45pm UTC](https://www.ruby-forum.com/t/an-association-problem-ar-base-find-include-and-n-deep-a/54949 "2006-03-15T13:45:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![juli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@juli](https://www.ruby-forum.com/u/juli)\
**Post date:** [March 15, 2006, 1:45pm UTC](https://www.ruby-forum.com/t/an-association-problem-ar-base-find-include-and-n-deep-a/54949/1 "2006-03-15T13:45:59Z")

</div>

Dear List,

I’m using Edge Rails.

I have a Model with Items for purchase. Each Item is of a certain brand.

Class Brand \< ActiveRecord::Base  
end

Class Item \< ActiveRecord::Base  
belongs\_to :brand  
end

The problem domain is that I’d like to create a separate Inventory  
list of Items. This includes the date of entry into inventory and  
date of exit from inventory (ie sale). Because each Item can  
potentially be sold back into the system, and we want to maintain a  
historical record, this next class (which I’m choosing to call  
InventoryItem) is not just a simple list of items - each item may  
have many records in this list. But each invoice\_item is only ever  
associated with ONE Item.

Class InventoryItem \< ActiveRecord::Base  
belongs\_to :item  
end

and Items gets modified to:

Class Item \< ActiveRecord::Base  
belongs\_to :brand  
has\_many :inventory\_items  
end

This is just fine and dandy, until I come to want to do searching  
with the AR::Base.find method… and I end up not being able to find  
based on brand. Obviously the :include =\> [] element of the options  
hash won’t work here, as I’ve tried, because it’s only looking at  
what I’m choosing to call two-deep association traversals.

I have obviously got the option to simply use Ruby to do this, but  
I’d rather use find, obviously, because it’ll generate proper SQL and  
I’d say it’d be much faster.

My quesiton is basically “Is there a way to do this elegantly using  
the find command and some option I’m not aware of as yet, or do I  
have to resort to using Ruby to do my pagination and limiting?”.

I’d like to be able to do stuff like InventoryItem.find  
(:all,'brand.name LIKE “%nike%”) or somesuch.

Thanks in advance,  
Julian.

---

<div class="post-metadata">

**Author:** ![juli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@juli](https://www.ruby-forum.com/u/juli)\
**Post date:** [March 17, 2006, 2:37am UTC](https://www.ruby-forum.com/t/an-association-problem-ar-base-find-include-and-n-deep-a/54949/2 "2006-03-17T02:37:55Z")

</div>

So I guess no one has a response to this?
