[英]how to deal with Intermediate MySQL tables on Ruby on rails?
我有一張廚師桌,一張飯桌和一個與廚師工作過的飯店相關的中間桌
|–––––––––––––––––| |–––––––––––––––––| |–––––––––––––––––|
| chef | | restaurant | | chef_restaurant |
|-----------------| |-----------------| |-----------------|
| chef_id | | restaurant_id | | restaurant_id |
| name | | name | | chef_id |
|_________________| |_________________| |_________________|
您需要腳手架廚師和餐館。 對於關系表,您不需要模型和控件,因此只需進行遷移即可創建關系表。 需要牢記兩件事,包括廚師和餐廳模型中的習慣關系。 並遵循關系表的命名約定,它必須同時具有兩個表名,且它們的表名都以升序排列。 您的關系表名稱必須為chefs_restaurants
Rails遷移允許輕松創建它,如下所示
rails g migration CreateJoinTable chefs restaurants
而在你的廚師模型中
has_and_belongs_to_many :restuarants
在您的餐廳模型中
has_and_belongs_to_many :chefs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.