# Need Help With Find(:all) and conditions

**URL:** <https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791>\
**Category:** Rails\
**Created:** [September 6, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791 "2006-09-06T20:34:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 6, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/1 "2006-09-06T20:34:47Z")

</div>

I have a USER model and a TAG model and they have a has\_and\_belongs\_to  
relationships between them.

I want to Tag.find(:all) … but I only want to return tags that have  
at least one association with at user.

Bonus geek points for the one who could improve on that by returning  
the top 50 tags (i.e. the most associations with users) but don’t  
return any that do not have at least one association with a user. 🙂  
(Will happen if only 30 tags in DB and only 25 have associations)

Thanks guys and gals 🙂

–  
John K.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 12:15am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/2 "2006-09-07T00:15:16Z")

</div>

To find the tags associated with a particular user, you could simple  
do:  
user = User.find(id) #or whatever parameter you’re using to find  
it.  
tags = user.tags  
This way you will only get the tags associated with the user.

To get the top 50, maybe you could have a “tagged” field in your table,  
that counts the times the user has been associated with a specific tag  
(this is just an idea… I don’t the DB schema). If you have a field  
like that, then you could do:  
tags = user.tags.find(:all, :order =\>“tagged DESC”)

Hope this can help.

Fernanda Aispuro

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 12:17am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/3 "2006-09-07T00:17:16Z")

</div>

I missed something…  
To limit the number of results to 50, just add:  
:limit =\> 50  
after the :order key.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 2:29am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/4 "2006-09-07T02:29:26Z")

</div>

Could you change the habtm to a has\_many :through and set up counter  
caches  
for the users and tags?

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 5:23am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/5 "2006-09-07T05:23:38Z")

</div>

On 9/7/06, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a USER model and a TAG model and they have a has\_and\_belongs\_to  
> relationships between them.
> 
> I want to Tag.find(:all) … but I only want to return tags that have  
> at least one association with at user.

I am assuming you are trying to do something like

Tag.find(:all, :include=\>:user]

:include will generate a left outer join, which means tags without  
users are returned as well. Try using a :join statement and make it an  
INNER JOIN. You’ll want to read up on the naming scheme for table  
aliases that AR uses, the Rdoc is under ActiveRecord::Associations.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 8:35am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/6 "2006-09-07T08:35:57Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

John K. wrote:

> Thanks everyone for you input. Sometimes SQL is just the nicest  
> solution. I found an almost perfect solution using Railsisms but it  
> just did not read as well as SQL. But I did wrap the SQL into a  
> method that I call from the controller so at least I am just upgrading  
> the dsl of my app. 🙂

I think inner join support should be added to the _find_ method options.  
I will gladly add it to the ActiveRecord Extensions if anyone else would  
find this useful.

What are your thoughts on the API of the following to do an INNER JOIN?  
Tag.find :all, :include=\>[:users\_must\_exist]  
OR  
Tag.find :all, :finclude=\>:users

The :finclude would be short for ‘force include’, meaning it would force  
that records existed in both tables, hence the INNER JOIN. Thoughts?

Zach D.  
Michigan Ruby U. Group - [http://www.gr-ruby.org](http://www.gr-ruby.org)  
ActiveRecord Extensions - [http://blogs.mktec.com/zdennis/pages/ARE](http://blogs.mktec.com/zdennis/pages/ARE)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFE/7NEMyx0fW1d8G0RAsSVAJ9ajdRAhPphQrvG5q9+X2AeBAgdQQCeK89+  
dTFza/hfKrOvb3sqx2bfNOc=  
=F22y  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 6:35am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/7 "2006-09-07T06:35:45Z")

</div>

Thanks everyone for you input. Sometimes SQL is just the nicest  
solution. I found an almost perfect solution using Railsisms but it  
just did not read as well as SQL. But I did wrap the SQL into a  
method that I call from the controller so at least I am just upgrading  
the dsl of my app. 🙂

On 9/6/06, Max M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
John K.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 7, 2006, 9:36am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/8 "2006-09-07T09:36:14Z")

</div>

> The :finclude would be short for ‘force include’, meaning it would force  
> that records existed in both tables, hence the INNER JOIN. Thoughts?

How about:

Tag.find :all, :must\_include =\> :users

Jason A.on  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 8, 2006, 3:06am UTC](https://www.ruby-forum.com/t/need-help-with-find-all-and-conditions/67791/9 "2006-09-08T03:06:35Z")

</div>

## ok i would use an association table like users\_tags so getting tags would be User\_tag.find(:all, :include =\> {:user, :tag}) getting the top x tags would be easy, just use a DISTINCT and order desc on first column

Heri R.  
[http://sprinj.com](http://sprinj.com)
