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.

No comments: