Showing posts with label activerecord. Show all posts
Showing posts with label activerecord. Show all posts

Wednesday, 23 May 2007

Improve Default Rails Database Performance

Active Record (AR) is slow. All that nice reflection comes at a price. You should use AR to prototype your app, then take a look at the logs.
1. Sort your development log.
If a database query is only run a few times, let AR handle it. If one is being run a 1000 times then we should pull it out and tweak it.
2. Pull the SQL from the log
Rails has already done the hard work for us, right?
3. Tweak the SQL
Stick the tweaked DB queries in your models and things should move along nicely...

Wednesday, 21 March 2007

Active Record Constructors

Do not write your own initialiser to instantiate the object. e.g:
class MyClass < ActiveRecord::Base
def initialize(params)
@params = params
end
end

Instead do this:

my_object = MyClass.new(:params => params)
my_object.save

Or this:

MyClass.create :params => params

The above will automatically save the record to the DB.