Rails 新手 2 个问题

anleb · 2012年09月08日 · 最后由 xworm 回复于 2012年09月10日 · 3145 次阅读

1.启用 mysql 为数据库服务器中的 database.yml reconnect: false 这个是什么意思? 2.我们什么时候会在数据库生成表,什么时候会生成数据库

就是 rails g controller 命令和 rails g module 命令,太混乱,想理清楚。

1.我的好像没有reconnect: false这句 2.同问

#1 楼 @w7938940 怎么会没有?

1,同问 2, rake db:create 生成数据库 rake db:migrate 生成表

rails g 这里 g 是简写 generate 生成的意思 然后的 controller model 只是对应不同的 rb 文件。根据这个 rails 帮助你将你的 rb 文件放置到 app 下面对应的文件夹

2.我们什么时候会在数据库生成表,什么时候会生成数据库 一般项目搭建起来就会生成数据库 然后当你完成你编写的第一个 migration 文件后会进行表的生成,对应的命令分别是 RAILS_ENV= xxxxx(环境) rake db:create / migrate

#2 楼 @Anleb 确实没有 #5 楼 @Shadow 是不是rails g controller不会生出相应的表,而rails g model会生出相应的表?

#6 楼 @w7938940 #5 楼 @Shadow #4 楼 @Shadow #3 楼 @jjym

1.对于 sqlite3 不说了,是默认数据库 2.对于 MYSQL,只有手动 rake db:create 才会生成数据库,而且是生成 development 和 test2 个数据库 3.对于生成表,是要执行 rake db:migrate 才会生成,对于 schema_migrations 也是执行了 rake 迁移任务后自动生成,全部实验得出

对于命令不命令应该和生成不生成 真实的表 没什么关系

reconnect: false 的问题,在 Rails Guides 上都有说明。

MySQL supports a reconnect flag in its connections – if set to true, then the client will try reconnecting to the server before giving up in case of a lost connection. You can now set reconnect = true for your MySQL connections in database.yml to get this behavior from a Rails application. The default is false, so the behavior of existing applications doesn’t change.

#6 楼 @w7938940 rails g controller 不涉及数据库 只是 MVC 里的控制器 rails g model 会生成一个 migration 的文件 你需要手动编辑这个 migration 文件 添加上你需要的字段/索引等等东西 然后执行

RAILS_ENV= xxxxx(环境) rake db:migrate

#8 楼 @chucai 原来默认就是 false,完全可以不写的嘛,受教了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号