导轨脚手架帮助

发布于 2024-08-09 06:06:09 字数 295 浏览 2 评论 0原文

说我想搭建大学

,但大学最终应该与国家相关。正如每所大学都有一个州,而一个州又拥有多所大学。

在我的脚手架命令中我应该包含 state_id 吗?

script/generate scaffold university name:string address:string state_id:int

...我知道我必须更新我的模型才能让他们理解这种一对多关系,但我不认为这是手动在数据库中添加列的最佳方法...所以我的问题是...什么是最合适的方法吗?我做得正确吗?请帮忙。

say I want to scaffold generate university

but university should eventually be related to state. as in each university has one state and one state has many universities.

in my scaffold command should I include state_id??

script/generate scaffold university name:string address:string state_id:int

... I know I will have to update my models to have them understand this one to many relationship but I dont think its the best way to just add the column in the database manually.... so my question is...what would be the most appropriate approach? am I doing it correctly? please help.

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

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

发布评论

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

评论(3

断舍离 2024-08-16 06:06:09

如您所知,最终应用程序并不需要依赖脚手架,它们只是提供一种方便的方法来自动生成视图的起点。

也许最简单的方法是让脚手架为将作为字段显示在视图中的列生成迁移。然后为那些不会的列创建第二个迁移。

script/generate scaffold university name:string address:string
script/generate migration add_status_id_to_universities state_id:integer another_hidden_column:string
rake db:migrate

即使您已经迁移过一次,此操作也有效。

PS 您可能需要查看 ActiveScaffold 插件,以自动使您的视图适应数据库更改。

As you know, scaffolds aren't meant to be relied on for a final application, they just provide a handy way to autogenerate a starting point for your views.

Probably the easiest way to do it, is to have scaffold generate the migration for the columns that will appear in your views as fields. Then create a second migration for those columns that won't.

script/generate scaffold university name:string address:string
script/generate migration add_status_id_to_universities state_id:integer another_hidden_column:string
rake db:migrate

This works even if you've already migrated once.

P.S. You might want to look at the ActiveScaffold plugin to automatically adapt your views to database changes.

江南烟雨〆相思醉 2024-08-16 06:06:09

我就是这样做的。所以是的,除非其他人有更好的方法。

That's the way I do it. So yes, unless someone else has a better way.

网白 2024-08-16 06:06:09

没有正确答案。您可以在创建脚手架时包含它,也可以跳过它并稍后在执行 rake db:migrate 之前将其添加到迁移中。

在脚手架中指定字段的优点是,Rails 可以添加反向迁移来删除该字段,然后自动生成的脚手架将在显示/编辑视图中包含该字段。

There's no right answer. You can include it when creating the scaffold or you can skip it and add it to the migration later before executing rake db:migrate.

The advantage of specifying the field in the scaffold is that Rails can add a reverse-migration to remove the field and then autogenerated scaffold will include the field in the show/edit view.

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