Rails 关于多对多关联的操作存在问题=.=

douya0808 · 2012年04月05日 · 最后由 douya0808 回复于 2012年04月05日 · 3950 次阅读

我先建了个表 students 主键 id name 又建了个表 corporations 主键 id name 建了个中间表 corporations_students 联合主键 student_id 和 corporation_id

然后分别生成 scaffold rails generate scaffold Student rails generate scaffold Corporation

这是问题出来了 我中间表的 scaffold 该怎么建呢? rails generate scaffold CorporationsStudent?CorporationStudent?这里的名字该打什么我就费解了=。=

scaffold 后面写表的单数,所以我糊涂这里的 student 和 corporation 中间表的单数该怎么算了

应该这个吧 rails generate scaffold CorporationsStudent
名称其实自己怎么叫都可以。。follow convention over configuration 的理念就好了。。。

我的中间表的命名符合命名规则了 但是这个 model 的名字直接给我整晕了=。=

这个中间表的 scaffold 是可以随便起的?

还想问个额外的问题 用命令:rails generate controller student index 会产生 student 控制器和 index 这个 action 以及 index.html.erb 并会自动形成路由 get "student/index"

但我现在想在已经存在的控制器的基础上再创建 action 及模板应该怎么办呢 我如果输入 rails generate controller student index2 就会提示 student 这个 controller 已经存在了=.=

求指导...

  1. 这个中间表,是 rails 内部用于实现多对多关联使用的 join table, 也不需要用页面来管理吧,用不着搞个 scaffold.

  2. 当提示存在时,是否覆盖,按提示来吧,选 n 不覆盖,让 rails 只创建新的文件, 完事后,在 controller 和 controller_test 中在补上相应的代码。

或者直接手工创建相应的文件,也不是什么麻烦事。

我觉得 rails generate 只是给了我们一个起点,就像一些 ide 的向导一样, 剩下的是我们自己的选择,但不能完全依赖它。

@xzgyb 嘿嘿 我之前都是手动网上添加的

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