I want to add a column to a table. So here is my migration. My question
is : Is it possible to say that this value must be between 1 and 7 and
must no be a float (1.4 shouldn’t be possible) ?
class AddValueToTable < ActiveRecord::Migration
def self.up
add_column :table, :value, :int
end
def self.down
remove_column :table, :value
end
end
def self.down
remove_column :table, :value
end
end
Well, if you define the column as an integer you’ll never end up with
1.4 as a value. It will always get truncated to an integer.
Depending on your database you can add those types of restrictions. I
don’t think rails migrations has support for it directly though.
You could also do it as a before_save validation in the model. You’ll
probably want to do this anyway so that if a user enters “1.4” you can
complain early.