# Find with condition on association

**URL:** <https://www.ruby-forum.com/t/find-with-condition-on-association/179922>\
**Category:** Rails\
**Created:** [December 8, 2009, 3:46am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922 "2009-12-08T03:46:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 8, 2009, 3:46am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/1 "2009-12-08T03:46:45Z")

</div>

I don’t know why I can never find this active record find-related stuff  
when I google for it. It slays me over and over again and I keep having  
to do ugly things to get around it.

If I have Articles and an article has\_many :comments

How do I do this??? (pseudo code)

@articles = Article.find(:all, :conditions =\> “articles that have 3 or  
more comments”)

thanks much,  
jp

P.S. Do I need to just shut down my Rails activities for a few days and  
learn how to write SQL and forget about having Rails do this for me?  
That seems to have been a “bait and switch” on becoming a decent web 2.0  
developer without ever having dealt with databases before. I did my  
best to learn rails, but it seems there are unspoken pre-requisites like  
this that keep biting me in the backside.

As an alternative to that, can anyone recommend some excellent sources  
of info on non-trivial Rails finds that include “:include” and “:joins”  
and “:select”. I can do all of the trivial stuff that shows up in all  
of the easily found examples. As soon as I get out of that “obvious”  
zone, I’m slayed time and time again.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 8, 2009, 5:35am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/2 "2009-12-08T05:35:04Z")

</div>

Jeff P. wrote:

> I don’t know why I can never find this active record find-related stuff  
> when I google for it.

Perhaps you should read the ActiveRecord::Base docs. 🙂

> It slays me over and over again and I keep having  
> to do ugly things to get around it.

Then you’re working too hard. ActiveRecord should be easy.

> If I have Articles and an article has\_many :comments
> 
> How do I do this??? (pseudo code)
> 
> @articles = Article.find(:all, :conditions =\> “articles that have 3 or  
> more comments”)

This is tougher than usual because you’re using an aggregate function on  
an associated table as a condition. I think you want

Article.find :all, :include =\> :comments, :conditions =\>  
[‘comments.count(\*) \> ?’, 3]

You might need :group =\> :id in there too.

> thanks much,  
> jp
> 
> P.S. Do I need to just shut down my Rails activities for a few days and  
> learn how to write SQL and forget about having Rails do this for me?

Probably not. But you _do_ need to know SQL in order to use  
ActiveRecord.

> That seems to have been a “bait and switch” on becoming a decent web 2.0  
> developer without ever having dealt with databases before.

You can’t be a decent data-driven Web app developer without  
understanding databases. Period. It is as essential as understanding  
HTML.

> I did my  
> best to learn rails, but it seems there are unspoken pre-requisites like  
> this that keep biting me in the backside.

It’s not really unspoken. It’s irresponsible for anyone to claim that  
you can use ActiveRecord without knowing SQL. Such claims do  
unfortunately get made, but only by people who do not know what they’re  
talking about. ActiveRecord will generate SQL for you, but you have to  
be able to understand the SQL.

> As an alternative to that, can anyone recommend some excellent sources  
> of info on non-trivial Rails finds that include “:include” and “:joins”  
> and “:select”. I can do all of the trivial stuff that shows up in all  
> of the easily found examples. As soon as I get out of that “obvious”  
> zone, I’m slayed time and time again.

Learn SQL. Your questions will be answered. AR uses SQL terminology,  
so it’s trivial if you know SQL and impenetrable if you don’t.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 8, 2009, 2:52pm UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/3 "2009-12-08T14:52:09Z")

</div>

Jeff P. wrote:

> Marnen Laibow-Koser wrote:  
> …\>
> 
> > Article.find :all, :include =\> :comments, :conditions =\>  
> > [‘comments.count(\*) \> ?’, 3]
> > 
> > You might need :group =\> :id in there too.  
> > Best,  
> > –Â  
> > MarnenÂ Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> Thanks much Marnen.
> 
> I’ve never seen this syntax before:  
> :conditions =\> [‘comments.count(\*) \> ?’, 3]  
> This is the sort of thing I can never find when I’m googling for help.  
> Never see it in any of the screen-casts or books either. It is like  
> “magic” that only the “cool kids” know about.

Because the cool kids know SQL.

> What is the meaning of the “(\*)” after “.count”??? If it was just  
> comments.count \> ?, 3 it would make sense to me. I don’t get the  
> asterisk thing at all.

Then look it up in your friendly local SQL reference.

> Curious, is :include required instead of :joins in order to use the AR  
> “count” method on comments?

Probably not. I was using it so as to try not to load all the comments  
data. And I’m not using the AR count method – that’s an SQL function.

In short: learn SQL. 🙂

> thanks,  
> jp

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 8, 2009, 8:45am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/4 "2009-12-08T08:45:45Z")

</div>

Marnen Laibow-Koser wrote:  
…\>

> Article.find :all, :include =\> :comments, :conditions =\>  
> [‘comments.count(\*) \> ?’, 3]
> 
> You might need :group =\> :id in there too.  
> Best,  
> –Â  
> MarnenÂ Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Thanks much Marnen.

I’ve never seen this syntax before:  
:conditions =\> [‘comments.count(\*) \> ?’, 3]  
This is the sort of thing I can never find when I’m googling for help.  
Never see it in any of the screen-casts or books either. It is like  
“magic” that only the “cool kids” know about.

What is the meaning of the “(\*)” after “.count”??? If it was just  
comments.count \> ?, 3 it would make sense to me. I don’t get the  
asterisk thing at all.

Curious, is :include required instead of :joins in order to use the AR  
“count” method on comments?

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 9, 2009, 8:03am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/5 "2009-12-09T08:03:23Z")

</div>

Jeff P. wrote:

> Marnen Laibow-Koser wrote:
> 
> > Jeff P. wrote:
> > 
> > > Marnen Laibow-Koser wrote:  
> > > …\>
> 
> “In short: learn SQL”
> 
> Thanks Marnen, that is, I think, the second meanest thing a guy has ever  
> said to me. 😉

I hope I can rely on that smiley…it was certainly not meant to be  
mean. 🙂

Good luck! You will find AR much easier to understand once you have a  
better idea of what it’s doing. You might find watching the SQL spew in  
the log to be educational too.

> cheers,  
> jp

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 9, 2009, 3:59am UTC](https://www.ruby-forum.com/t/find-with-condition-on-association/179922/6 "2009-12-09T03:59:29Z")

</div>

Marnen Laibow-Koser wrote:

> Jeff P. wrote:
> 
> > Marnen Laibow-Koser wrote:  
> > …\>

“In short: learn SQL”

Thanks Marnen, that is, I think, the second meanest thing a guy has ever  
said to me. 😉

cheers,  
jp
