# Best way to test for existence in a joined model

**URL:** <https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645>\
**Category:** Rails\
**Created:** [September 17, 2009, 12:48am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645 "2009-09-17T00:48:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 12:48am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/1 "2009-09-17T00:48:06Z")

</div>

Hi all, given these relationships

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

class Item \< ActiveRecord::Base  
has\_many :orders

@items = Item.find( :all )

@items.each do |item|  
…

What’s the best way to test for existence of an “item” in the Order  
database?

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 1:17am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/2 "2009-09-17T01:17:22Z")

</div>

On Wed, Sep 16, 2009 at 5:46 PM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @items.each do |item|  
> …
> 
> What’s the best way to test for existence of an “item” in the Order  
> database?

@order = Order.find(:first, :conditions =\> ‘whatever’, :include =\>  
:items )

@item = @order.items.find( :first, :conditions =\> ‘whatever’ ) if @order

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 2:35am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/3 "2009-09-17T02:35:12Z")

</div>

Thanks super, Greg, for helping me out. I’m dense, 'cause I couldn’t  
figure out the syntax for ‘whatever’. I did get this to work in  
the .erb file:

\<% if Item.find( item ).orders.size \> 0 %\>  
\<%=h “Found #{item.title} in Orders” %\>  
\<% end %\>

But I’m just wondering… is that the most efficient way to do it? Is  
there a more efficient way?

Thanks for putting in the time to help this noob out,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 3:13am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/4 "2009-09-17T03:13:50Z")

</div>

On Wed, Sep 16, 2009 at 7:33 PM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks super, Greg, for helping me out. I’m dense, 'cause I couldn’t  
> figure out the syntax for ‘whatever’. I did get this to work in  
> the .erb file:
> 
> \<% if Item.find( item ).orders.size \> 0 %\>

I’d go with orders.empty?

You will want to do your find in the controller. Set the results to  
an instance variable.

@item = Item.find(…)

Then that instance variable is available in your view:

\<%= debug @item %\>

> ```
> <%=h "Found #{item.title} in Orders" %>
> 
> ```
> 
> \<% end %\>
> 
> But I’m just wondering… is that the most efficient way to do it? Is  
> there a more efficient way?

Depends on your app. You might want that :include =\> :items or you  
might not. It’ll save a bunch of smaller queries later if you leave  
it off, but then what if you needed all those items? It’s up to you.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 9:37am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/5 "2009-09-17T09:37:49Z")

</div>

2009/9/17 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks super, Greg, for helping me out. Â I’m dense, 'cause I couldn’t  
> figure out the syntax for ‘whatever’. Â I did get this to work in  
> the .erb file:
> 
> \<% if Item.find( item ).orders.size \> 0 %\>

What is item here, is it already an Item object? If so then all you  
need is  
if item.orders.empty?  
No need to find it again.

Arguably better would be to provide a method in Item, possibly  
‘orders?’ that indicates whether that item has any orders. Then you  
would use  
if item.orders?  
where method orders? of Item returns self.orders.empty?  
This moves logic from the view into the model which is often worthwhile.

Whenever I see a.b.c I consider whether something should be moved into a  
model.

Colin

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 17, 2009, 9:29am UTC](https://www.ruby-forum.com/t/best-way-to-test-for-existence-in-a-joined-model/175645/6 "2009-09-17T09:29:37Z")

</div>

2009/9/17 Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > \<% end %\>
> > 
> > But I’m just wondering… is that the most efficient way to do it? Â Is  
> > there a more efficient way?
> 
> Depends on your app. Â You might want that :include =\> :items or you  
> might not. Â It’ll save a bunch of smaller queries later if you leave  
> it off, but then what if you needed all those items? Â It’s up to you.

Isn’t that the wrong way round?

Colin
