python



The power and pain of ActiveRecord…

Update: Thanks to the posts, I stand corrected. Rails R Best. That is all! :)

Ok, this came up in discussion last night when we were hanging out at Cafe Gecko after the Refresh Dallas meeting. I like the speed and flexibility of writing against the ActiveRecord system, and inside the MVC model. It makes coding less work than the design, which is certainly new for those of us coming from old-school PHP and Python coding. Take this example:

There are four tables that are used regularly in SimpleTicket:

  • Tickets
  • Users
  • Journals
  • Customers

The tables are all tied like so; Tickets has_many Users, Journals. Customers has_many Users. etc. etc. etc. So when finding the name of a user who owns a certain ticket, I can write:

< %= Ticket.find_by_id(id).user.name %>

However, there is a minor concern with this. That simple statement, while fun to write, generates two SELECT statements against the SQL database. Seems kinda like overkill? It gets worse. There are many different ways that I use this model set to paint the pages shown in the admin utility, and the My Tickets page can churn out some really heavy SELECT statements, in large numbers. So while it is easy to code, how do you control the sheer dearth of queries to make your application behave responsibly? Yes, you can do things like create objects on the fly with find_by_sql and such, but that really takes away the power of the MVC model.

If any of you have any thoughts on this, I would love to hear them.

January 12, 2006 | Trackback | [9] Comments

Tags: SimpleTicket , Open Source , Trouble Ticket , Ajax , Ajaxian , Ruby on Rails , activerecord , python , php , mvc | Bookmark on del.icio.us | Digg It