# Query pulling too many fields

**URL:** <https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757>\
**Category:** Rails\
**Created:** [April 17, 2009, 2:58am UTC](https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757 "2009-04-17T02:58:38Z")\
**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:** [April 17, 2009, 2:58am UTC](https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757/1 "2009-04-17T02:58:38Z")

</div>

i’m a little confused here on what’s happening.

i have two tables ITEMS and ITEM\_TYPES

ITEMS (belongs\_to :item\_type)  
id  
name  
item\_type\_id

ITEM\_TYPES  
id  
name

here is my Item.rb model code for my query:

```
find(:all,
     :select => 'items.name, item_types.name',
     :include => :item_type,
     :conditions => ['item_types.name = ? OR item_types.name = ? OR

```

item\_types.name = ? OR item\_types.name = ?’, ‘Material’, ‘Weapon’,  
‘Armor’, ‘Accessory’],  
:order =\> ‘items.name’)

currently it is selecting all fields in the ITEM table and not using the  
:select that i have entered.

if i comment out the :conditions parameter the :select works properly.  
i’m sure it’s something to do with the linking to the item\_types table.  
does anyone know how i can get the above :select working properly?

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:** [April 17, 2009, 3:31am UTC](https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757/2 "2009-04-17T03:31:39Z")

</div>

The find’s :select option is ignored when you combine it with  
the :include option.

On Apr 16, 8:58 pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [April 17, 2009, 3:49am UTC](https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757/3 "2009-04-17T03:49:52Z")

</div>

Harold wrote:

> The find’s :select option is ignored when you combine it with  
> the :include option.
> 
> On Apr 16, 8:58ï¿½pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

thanks. i removed the include and did my joins manually. i guess  
that’s probably the best way:

```
find(:all,
     :select => 'item_types.name, items.id, items.name',
     :joins => 'join item_types on items.item_type_id = 

```

item\_types.id’,  
:conditions =\> [‘item\_types.name = ? OR item\_types.name = ? OR  
item\_types.name = ? OR item\_types.name = ?’, ‘Material’, ‘Weapon’,  
‘Armor’, ‘Accessory’],  
:order =\> ‘items.name’)

---

<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:** [April 18, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/query-pulling-too-many-fields/165757/4 "2009-04-18T23:36:13Z")

</div>

The whole operation smells like a somewhat premature operation, unless  
either Item or ItemType has a whole bunch of fields you didn’t  
mention. Why not just let AR work the way it does?

Item.find(:all, :include =\> :item\_type, :conditions =\>  
{ :‘item\_types.name’ =\> %w(Material Weapon Armor Accessory) })

(that last bit changes the chain of 'OR’s above into an IN statement,  
which is a cheap optimization, and easier to change later…)

–Matt J.

On Apr 16, 9:49 pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
