开发者

Does rails scaffold command support generate belongs_to or many to many model middle table migration info?

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

开发者_C百科
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.


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 .


rails g model ProductCategories product:references category:references
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜