如何声明迁移中的belongs_to不能为空?



是否可以声明belongs_to类型的字段不能为空?现在我有以下迁移:

class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.belongs_to :site
t.string :title
t.timestamps
end
end
end

生成一个表,其中site_id可以为 null。

您可以向belongs_to添加选项,因此您可以在此处添加null:false选项。

class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.belongs_to :site, null: false
t.string :title
t.timestamps
end
end
end

最新更新