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