To me, the most painful part of working with Ruby on Rails has been the immaturity of the supporting documentation, specifically as regards ActiveRecord. I know there are boatloads of Ruby on Rails books on the way, and to me, the one that’s most needed is an in depth explanation of how ActiveRecord works and what you can do with it. I’m also beginning to wonder if ActiveRecord isn’t perhaps a bit thinner than I originally gave it credit for. It works brilliantly for all of the simple stuff I was doing initially, but now that I’m moving to the next level of complexity, I find myself a bit lost in the wilderness.
First, let me say that ActiveRecord supports a mechanism called “find by SQL,” which basically lets you create join clauses and where clauses in any way that you choose. What I’m trying to figure out is whether I have to go that route for a large class of queries that would seem to be common to many types of applications. Let me give you an example. You have two tables in the database called
categories. (I call them that because that’s really what they’re named in my application. They have a many to many relationship with one another, so I have another table called
categories_questions. Let’s say that I want to create a page that lists all of the uncategorized questions. I could write a SQL statement like this:
select * from questions where not exists (select question_id from categories_questions where question_id = question.id)
If I preferred, i could do the same thing with a left outer join, like this:
select q.* from questions q left outer join categories_questions cq on q.id = cq.question_id where cq.category_id is null
It strikes me that I should be able to do both of these things within ActiveRecord without reproducing most of those queries using
find_by_sql. You wouldn’t know it though from any of the online resources I’ve looked at, or the Rails book from the Pragmatic Programmers, or any of the open source Rails applications that I’ve browsed.
I had forgotten how much fun being an early adopter can be.