Railsscaffold命令是否支持生成belongs_to或多对多模型中间表迁移信息?

发布于 2024-09-29 15:57:24 字数 303 浏览 4 评论 0原文

产品,类别是rails3上的两个模型,它们之间的关系如下:

产品 has_and_belongs_to_many 类别

类别 has_and_belongs_to_many 产品

我可以使用脚手架为这两个模型使用生成迁移

rails g scaffold product name:string
rails g scaffold category name:string

,但是如何生成多对多模型的中间表迁移信息,或者我需要手动写,如果是的话这对我来说很难,希望有人能帮助我。

Product,Category is two model on rails3 the relation between them are follow:

product has_and_belongs_to_many categories

category has_and_belongs_to_many products

i can use scaffold generate migration for this two modle use

rails g scaffold product name:string
rails g scaffold category name:string

but how can i generate the many to many model's middle table migration info,or i need write it manually,if so this is hard for me,hope someone could help me.

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

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

发布评论

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

评论(2

蒗幽 2024-10-06 15:57:24

您需要自己创建此表

   create_table :products_categories, :id => false do |t| 
     t.integer :product_id 
     t.integer :category_id
   end

警告,您需要将 :id 定义为 false,因为此表不需要 id 列。如果您有 id 列,则该表无法在 has_and_belongs_to_many 上使用。

You need create this table by yourself

   create_table :products_categories, :id => false do |t| 
     t.integer :product_id 
     t.integer :category_id
   end

Warning, you need define the :id to false, because this table no need id column. If you have an id column, the table is invalid to be used on has_and_belongs_to_many .

泪痕残 2024-10-06 15:57:24
rails g model ProductCategories product:references category:references
rails g model ProductCategories product:references category:references
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文