# How do you check if associated table is empty from find?

**URL:** <https://www.ruby-forum.com/t/how-do-you-check-if-associated-table-is-empty-from-find/160498>\
**Category:** Rails\
**Created:** [February 17, 2009, 2:36am UTC](https://www.ruby-forum.com/t/how-do-you-check-if-associated-table-is-empty-from-find/160498 "2009-02-17T02:36:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 17, 2009, 2:36am UTC](https://www.ruby-forum.com/t/how-do-you-check-if-associated-table-is-empty-from-find/160498/1 "2009-02-17T02:36:23Z")

</div>

So I have two tables:

- teams
- players

I’m trying to find all teams that have no players. I thought this could  
work:

Team.find(:all, :joins =\> :players, :conditions =\> [“players.size = ?”,  
0] )

Of course that’s not working for me because I’m mixing the SQL with  
Rails. Do you know the proper way to find all teams that have no  
players?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 17, 2009, 2:47am UTC](https://www.ruby-forum.com/t/how-do-you-check-if-associated-table-is-empty-from-find/160498/2 "2009-02-17T02:47:20Z")

</div>

You could add a :counter\_cache to your model if you want a simple  
solution:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

On Feb 16, 8:36 pm, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
