导轨脚手架帮助
说我想搭建大学
,但大学最终应该与国家相关。正如每所大学都有一个州,而一个州又拥有多所大学。
在我的脚手架命令中我应该包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如您所知,最终应用程序并不需要依赖脚手架,它们只是提供一种方便的方法来自动生成视图的起点。
也许最简单的方法是让脚手架为将作为字段显示在视图中的列生成迁移。然后为那些不会的列创建第二个迁移。
即使您已经迁移过一次,此操作也有效。
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.
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.
我就是这样做的。所以是的,除非其他人有更好的方法。
That's the way I do it. So yes, unless someone else has a better way.
没有正确答案。您可以在创建脚手架时包含它,也可以跳过它并稍后在执行 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.