Joe
Joe

Reputation: 15331

How to specify the size of an integer in the migration script

We have two columns which require to be declared as t.integer size(1) and size(2). i.e. a max size of 1 (i.e upto 9) and max size of 2 (i.e upto 99). How should I declare this in my migration script.

Upvotes: 4

Views: 16962

Answers (2)

Chris Lewis
Chris Lewis

Reputation: 1325

Do you mean that the value in that column should be restricted to the range 1-99?

Having created an integer column you could add ActiveRecord validation to the model:

validates_numericality_of :field_name, :in => 1..99

Upvotes: 18

imgrgry
imgrgry

Reputation: 628

You should be able to set a :limit on your migration record. Check the documentation here -- http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column

Ex: add_column :my_tbl, :myint, :integer, :limit => 9

This will set a column length -- in other words, it will only allow integers up to 9 digits long.

If you want to restrict the data input for this column, you'll need to do validations in your model. Have a look at http://guides.rubyonrails.org/active_record_validations_callbacks.html#length

Upvotes: -7

Related Questions