# Create new model - add custom defaults to migration?

**URL:** <https://www.ruby-forum.com/t/create-new-model-add-custom-defaults-to-migration/113606>\
**Category:** Rails\
**Created:** [September 2, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/create-new-model-add-custom-defaults-to-migration/113606 "2007-09-02T19:28:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessehk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jessehk](https://www.ruby-forum.com/u/jessehk)\
**Post date:** [September 2, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/create-new-model-add-custom-defaults-to-migration/113606/1 "2007-09-02T19:28:07Z")

</div>

I am new to Ruby and Rails, have been messing about with migrations  
mostly, was trying to create a migration file that would automattically  
add certain columns and triggers to each table in the database, made  
some progress with that but now rethinking my approach

when creating a new model/migration file, is it possible to get the  
generator to add in default values; in my case I would like a few  
columns such as created\_on, modified\_on, and some triggers to update  
these values

so given a model Project, the following migration file is generated via  
ruby script/generate model Project

```auto
class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :projects do |t|
    end
  end

  def self.down
    drop_table :projects
  end
end
```

but is it possible to have the generated file automatically load in  
additional code? i.e.

```auto
class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :projects do |t|
      t.column :created_on, :datetime
      t.column :modified_on, :datetime
    end

    sql = 'CREATE TRIGGER projects_before_insert BEFORE INSERT ON
projects FOR EACH ROW SET NEW.created_on = NOW(); '
    execute(sql)

  end

  def self.down
    drop_table :projects
  end
end
```

Thanks in advance for any help or pointers

- J

---

<div class="post-metadata">

**Author:** ![jessehk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jessehk](https://www.ruby-forum.com/u/jessehk)\
**Post date:** [September 2, 2007, 8:57pm UTC](https://www.ruby-forum.com/t/create-new-model-add-custom-defaults-to-migration/113606/2 "2007-09-02T20:57:46Z")

</div>

ok - after further Google searches came across this great site  
‘Railscasts’  
‘How to Make a Generator’ - [#58 How to Make a Generator - RailsCasts](http://railscasts.com/episodes/58)

armed with a better understanding of how a generator works  
I took a look at the model\_generator.rb and /templates/migration.rb code

well a quick hack of the /templates/migration.rb code and I am on my way  
BUT - this just feels down right dirty hacking this template directly;  
but it is very effective

on windows XP with Ruby in installed at C:\Ruby  
the path to this file is  
C:\ruby\lib\ruby\gems\1.8\gems\rails-1.2.3\lib\rails\_generator\generators\components\model\templates

# the hacked version - created\_on, modified\_on columns and triggers

(mysql)  
class \<%= migration\_name %\> \< ActiveRecord::Migration  
def self.up  
create\_table :\<%= table\_name %\> do |t|  
\<% for attribute in attributes -%\>  
t.column :\<%= attribute.name %\>, :\<%= attribute.type %\>  
\<% end -%\>  
t.column :created\_on, :datetime  
t.column :modified\_on, :datetime  
end

```
sql = 'CREATE TRIGGER <%= table_name %>_before_insert BEFORE INSERT 

```

ON \<%= table\_name %\> FOR EACH ROW SET NEW.created\_on = NOW(),  
NEW.modified\_on = NOW(); ’  
execute(sql)

```
sql = 'CREATE TRIGGER <%= table_name %>_before_update BEFORE UPDATE 

```

ON \<%= table\_name %\> FOR EACH ROW SET NEW.modified\_on = NOW(); ’  
execute(sql)  
end

def self.down  
drop\_table :\<%= table\_name %\>  
end  
end
