生成许多​​模型来许多铁轨3 [英] Generating a model with many to many in rails 3

查看:138
本文介绍了生成许多​​模型来许多铁轨3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法产生具有许多铁轨模型一对多关系predefined?我知道如何在事后将其添加到活动的记录,但它会是不错的它在数据库迁移和活动记录模式马上蝙蝠定义。

Is there a way to generate a rails model with a many to many relationship predefined? I know how to add it to the active record after the fact but it would be nice to have it defined in the db migration and the active record model right off the bat.

推荐答案

请记住,你不想要一个ID为连接表,所以一定要补充:ID =>假| T |

Remember that you do not want an id for the join table, so make sure to add :id => false |t|

create_table assemblies_parts, :id => false do |t|
  t.integer :assembly_id
  t.integer :part_id
end

如果您使用的轨道

rails generate model Assemblies_parts assembly:references part:references

您将有两个指标,但你想要的是

you will have two indexes, but what you want is

# Add table index
add_index :assemblies_parts, [:assembly_id, :part_id], :unique => true

这篇关于生成许多​​模型来许多铁轨3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆