从命令行生成带有必填/必填字段(即非空)的 Rails 模型 [英] Generate Rails models with mandatory/required fields (i.e not null) from command line

查看:28
本文介绍了从命令行生成带有必填/必填字段(即非空)的 Rails 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮忙生成带有必填字段/列(即非空)的 Rails 模型吗?例如,

Can anyone please help with generating Rails models with mandatory fields/columns (i.e. NOT NULL)? For example,

$rails generate model Role name:string <???>

我需要指定什么才能获得如下所示的null: false"约束?

What do I need to specify in order to get the "null: false" constraint as shown below?

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      **t.string :name, null: false**

      t.timestamps
    end
  end
end

提前致谢

推荐答案

您不能在生成器命令中执行此操作.不过,将 null: false 添加到迁移文件中非常简单.

You can't do that in a generator command. It's quite simple to add null: false to your migration file though.

这篇关于从命令行生成带有必填/必填字段(即非空)的 Rails 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆