Find with condition on association

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.

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. :slight_smile:

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
[email protected]

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
[email protected]

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. :slight_smile:

thanks,
jp

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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
[email protected]

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

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. :wink:

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

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
[email protected]

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. :wink:

cheers,
jp