I know that I can generate a model and set a constraint such as uniq by doing "rails generate model field1:string:uniq", for example. Is there a way I can set a "not null" constraint?
Asked
Active
Viewed 973 times
0
-
Related question: http://stackoverflow.com/questions/4562677/passing-additional-parameters-to-rails-generate-model – Andrew Grimm Jul 15 '15 at 00:46
1 Answers
0
I don't think you can do that while running the generator. Have you considered changing the migration file after your model is generated?
Migration files are inside db/migrations, and you could make a field as not nullable by adding a few parameters to your migration line, before running rake db:migrate.
Suppose you have a column named category_id:
t.integer :category_id, null: false, default: 0
This would create the field in the database in a not null format.

Marcelo Ribeiro
- 1,718
- 1
- 13
- 27