# Optional Include Relationship

**URL:** <https://www.ruby-forum.com/t/optional-include-relationship/54073>\
**Category:** Rails\
**Created:** [February 22, 2006, 7:41am UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073 "2006-02-22T07:41:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 7:41am UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/1 "2006-02-22T07:41:47Z")

</div>

I did some searches in the mailing list and on the web but I can’t  
seem to find the answer to this, but it’s pretty much a noob question.

I have a Product object

class Product \< ActiveRecord::Base  
has\_many :product\_identifications  
has\_one :product\_identification\_item\_number  
has\_one :product\_identification\_product\_code  
end

the product\_identifications is using STI to have the two optional  
product identifiers Item Number and Product Code.

I would like to use this on a listing screen but use :include to keep  
it at only one query, however when i add “:include  
=\> :product\_identification\_item\_number” i only get back records where  
an Item Number exists for the product. I did try including the base  
product\_identifications, which doesn’t limit the results but wouldn’t  
be the cleanest way for my presentation.

Any help would be greatly appreciated,  
Dave

---

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/2 "2006-02-22T16:40:10Z")

</div>

> it at only one query, however when i add “:include  
> =\> :product\_identification\_item\_number”

@products.find(:all)

for product in @products  
product.product\_identification\_item\_number

---

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/3 "2006-02-22T17:49:55Z")

</div>

Anthony G. wrote:

> > it at only one query, however when i add “:include  
> > =\> :product\_identification\_item\_number”

@products.find(:all, :include =\> :product\_identification\_item\_number)

for product in @products  
product.product\_identification\_item\_number

should work

Whats is the sql being generated in the development log ?

\_Tony

---

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/4 "2006-02-22T20:00:18Z")

</div>

Thinking about this a little more, I’m guessing this only affects  
models using STI, as typically there wouldn’t be the additional WHERE  
statement. Is this possibly addressed in EdgeRails?

Dave

---

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/5 "2006-02-22T20:00:18Z")

</div>

Hi Antony,

Here is the generated SQL:

SELECT product\_identifications.`id` AS t1\_r0,  
product\_identifications.`product_id` AS t1\_r1, products.`id` AS  
t0\_r0, product\_identifications.`type` AS t1\_r2, products.`name` AS  
t0\_r1, product\_identifications.`id_value` AS t1\_r3 FROM products LEFT  
OUTER JOIN product\_identifications ON  
product\_identifications.product\_id = products.id WHERE  
(product\_identifications.`type` = ‘ProductIdentificationItemNumber’ )  
LIMIT 0, 10e

It definitely will only return products that have an associated  
product\_identification\_item\_number, if the “WHERE  
(product\_identifications.`type` =  
‘ProductIdentificationItemNumber’ )” was located in the join it would  
make it optional.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![David\_Krupinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de0a6/32.png) [@David\_Krupinski](https://www.ruby-forum.com/u/David_Krupinski)\
**Post date:** [February 22, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/optional-include-relationship/54073/6 "2006-02-22T22:48:05Z")

</div>

Dave,

Check out [http://wiki.rubyonrails.org/rails/pages/Allow+Multiple](http://wiki.rubyonrails.org/rails/pages/Allow+Multiple)  
+Associations+Same+Table+Plugin

With that in place, Rails 1.0 will correctly generate the conditions  
on the JOIN.

Edge Rails doesn’t yet solve the same problems as this plugin, but it  
looks like it’s moving closer to doing so.

Cheers,

Pete Y.
