Follow Along as I Stumble on the Path to Learning RoR

Setting Default Values for Rails Fields

For some fields, you may want to set default values for when a new record is created.  For example, if you want to set a flag/boolean field to true or false when a new record is created.  An easy way to do this is through a migration.

If you are just creating the field, you can do:

def change
add_column :table_name, :field_name, :field_type, :default => 0
add_column :table_name, :field_name, :field_type, :default => true
end

To edit an existing field:

def up
change_column :table_name, :field_name, :boolean, :default => true
end

Advertisements

Comments are closed.