To set a non-zero restriction at the database level, add null: falseActiveRecord to the migration. For example,
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title, null: false
t.string :content, null: false
t.string :author, null: false
t.timestamps
end
end
end
You should also (or can alternatively) add a presence validator to your model that works at the Rails level and provides error messages to end users:
class Post < ActiveRecord::Base
validates :title, :content, :author, presence: true
end
. Rails Guides .