Ruby is an amazingly productive language, and Rails is an awesome framework. However, you need to be aware of what you are getting into. There are lot’s of items that make life difficult for your web server. Some of the biggest culprits are has_one, has_many, etc. They are so powerful, but use them wrong and your database will be in trouble.
For example:
class Company < ActiveRecord::Base
has_many :people
# There is a name column in the db
end
class Person < ActiveRecord:Base
belongs_to :company
end
Now, assume you will be showing 50 people with their company names on a page. The view code is very simple:
<% @people.each do |p| %>
<%= p.first_name + “, ” p.company.name %>
<% end %>
Forget about the formating, but this simple loop will result in 50 database calls to retrieve the company name. If your application does this a lot, you will need some caching, or your database will be in trouble.
It’s a good idea to watch your logs and attack those areas that are performing excessive database queries.
Leave a Reply