# Setting find conditions to equal a value 2 tbl joins away

**URL:** <https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201>\
**Category:** Rails\
**Created:** [May 21, 2008, 1:05am UTC](https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201 "2008-05-21T01:05:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 21, 2008, 1:05am UTC](https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201/1 "2008-05-21T01:05:46Z")

</div>

ok here is my initial table setups:

ITEM TYPES  
id  
name

ITEMS  
id  
name  
item\_type\_id

TOOLS  
id  
item\_id  
parent\_id

class Tool \< ActiveRecord::Base  
belongs\_to :item

```
def name
  item.name
end

def item_type
  item.item_type.name
end

```

end

class Item \< ActiveRecord::Base  
belongs\_to :item\_type  
end

I can use the following tool.item\_type or item.item\_type.name to get  
the name of the item type but i can not get this query to work:

@tools = Tool.find(:all, :include =\> :item, :conditions =\> { :item\_type  
=\> “Instrument”}, :order =\> :name)

I am getting Unknown column ‘tools.item\_type’ even though item\_type is  
defined in the model. any ideas?

thanks!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 21, 2008, 1:18am UTC](https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201/2 "2008-05-21T01:18:35Z")

</div>

On 21 May 2008, at 00:05, Scott K. wrote:

> end  
> { :item\_type  
> =\> “Instrument”}, :order =\> :name)
> 
> I am getting Unknown column ‘tools.item\_type’ even though item\_type is  
> defined in the model. any ideas?

conditions need to reference database columns. methods you’ve defined  
don’t count.  
:conditions =\> {‘items.item\_type’ =\> ‘Instrument’} should work.

Fred

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 21, 2008, 1:46am UTC](https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201/3 "2008-05-21T01:46:28Z")

</div>

Frederick C. wrote:

> On 21 May 2008, at 00:05, Scott K. wrote:
> 
> > end  
> > { :item\_type  
> > =\> “Instrument”}, :order =\> :name)
> > 
> > I am getting Unknown column ‘tools.item\_type’ even though item\_type is  
> > defined in the model. any ideas?
> 
> conditions need to reference database columns. methods you’ve defined  
> don’t count.  
> :conditions =\> {‘items.item\_type’ =\> ‘Instrument’} should work.
> 
> Fred

ah ok thanks. since item\_type isn’t directly a database column but  
item\_type\_id is i can get this statement working:  
@tools = Tool.find(:all, :include =\> :item, :conditions =\>  
{‘items.item\_type\_id’ =\> 2}, :order =\> :name)

but what if i want to go one level deeper like

```
@tools = Tool.find(:all, :include => :item, :conditions => 

```

{‘items.item\_types.name’ =\> ‘Instrument’}, :order =\> :name)

or

```
@tools = Tool.find(:all, :include => :item, :include => :item_type, 

```

:conditions =\> {‘item\_types.name’ =\> ‘Instrument’}, :order =\> :name)

the first give me an error saying Unknown column ‘items.item\_types.name’  
the second gives me the error Association named ‘item\_type’ was not  
found.

thanks for the help so far. i think i’m almost there!

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [May 21, 2008, 8:21am UTC](https://www.ruby-forum.com/t/setting-find-conditions-to-equal-a-value-2-tbl-joins-away/138201/4 "2008-05-21T08:21:50Z")

</div>

On 21 May 2008, at 00:46, Scott K. wrote:

> or
> 
> @tools = Tool.find(:all, :include =\> :item, :include =\> :item\_type,  
> :conditions =\> {‘item\_types.name’ =\> ‘Instrument’}, :order =\> :name)
> 
> the first give me an error saying Unknown column  
> ‘items.item\_types.name’  
> the second gives me the error Association named ‘item\_type’ was not  
> found.

There’s 2 separate things going on here. The first is including/  
joining the right tables (by the way, do you need :include or  
is :joins enough)  
if you need to join item and then join item\_types onto that then you  
need to do :include =\> {:item =\> :item\_type}.  
The second bit is the condition, where you just need to provide the  
disambiguated column name ie {‘item\_types.name’ =\> ‘Instrument’}

Fred
