在 Rails 中定义可空字段和外键时的脚手架
我只是想办法解决 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
脚手架仅生成您随后运行的迁移。文件生成后,只需打开生成的迁移并调整您需要特定约束的任何值。默认情况下,列设置为空,除非您另外指定,例如:
这是由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.:
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.