在 Rails 中定义可空字段和外键时的脚手架

发布于 2024-09-30 06:11:01 字数 392 浏览 1 评论 0原文

我只是想办法解决 Rails 问题,但我需要使用 railsgeneratescaffold 命令获得一些帮助。

这是我想要使用的命令,

rails generate scaffold Expense user:??? name:string description:text

我希望 description 字段可为空,并且 users 字段链接到另一个模型 - 在本例中我'我想为用户创建一个外键。我正在使用 devise 身份验证框架。

我读到,许多 RoR 开发人员尝试避免使用脚手架方法,而是选择手动方法,但我的网络应用程序非常简单,我想过采用脚手架方法。

I'm just figuring out my way around rails but I need a little help with the rails generate scaffold command.

Here's the command that I'd like to use

rails generate scaffold Expense user:??? name:string description:text

I'd like the description field to be nullable and the users field to be linked to another Model — in this case I'd like to create a foreign key to the Users. I'm using the devise authentication framework.

I've read that many RoR developers try and avoid the scaffolding method and opt for the manual approach instead but my web-app is quite simple and I've thought of going the scaffolding way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-10-07 06:11:01

脚手架仅生成您随后运行的迁移。文件生成后,只需打开生成的迁移并调整您需要特定约束的任何值。默认情况下,列设置为空,除非您另外指定,例如:

  create_table "slugs", :force => true do |t|
    t.integer  "sequence",                     :default => 1, :null => false
    t.string   "sluggable_type", :limit => 40
    t.string   "scope",          :limit => 40
    t.datetime "created_at"
  end

这是由Friendly_id插件生成的代码,您可以看到它们指定了序列列不能为空,而其他字段有其他约束。

Scaffolding only generates the migration that you then run. Once the file is generated simply crack open the generated migration and adjust any of the values you need specific constraints on. By default columns are set to null unless you specify otherwise e.g.:

  create_table "slugs", :force => true do |t|
    t.integer  "sequence",                     :default => 1, :null => false
    t.string   "sluggable_type", :limit => 40
    t.string   "scope",          :limit => 40
    t.datetime "created_at"
  end

This is the code generated by the friendly_id plugin as you can see they have specified that the sequence column cannot be null while the other fields have other constraints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文