如何处理 rails 上 Ruby 上的中级 MySQL 表?

how to deal with Intermediate MySQL tables on Ruby on rails?

我有一个 table 厨师,一个 table 餐厅和一个中级 table 与厨师工作过的餐厅有关

|–––––––––––––––––|  |–––––––––––––––––|  |–––––––––––––––––|
| chef            |  | restaurant      |  | chef_restaurant |
|-----------------|  |-----------------|  |-----------------|
| chef_id         |  | restaurant_id   |  | restaurant_id   |
| name            |  | name            |  | chef_id         |
|_________________|  |_________________|  |_________________|
  1. 如果我在 rails 中使用脚手架,我应该为 3 个 table 生成脚手架吗?在你的 SQL 模型中有中间 tables 的事实是否会损害使用脚手架?
  2. 您查询这些中间 table 的方法是什么?

您需要搭建厨师和餐厅的脚手架。对于关系 table,您不需要模型和控件,因此只需迁移以创建关系 table 就足够了。需要记住两件事,包括厨师和餐厅模型中的习惯关系。并且遵循您的关系 table 的命名约定,它必须具有两个 table 名称的复数形式,并按升序排列。你的关系table名字必须是chefs_restaurants

Rails 迁移允许像下面这样轻松创建它

rails g migration CreateJoinTable chefs restaurants 

在你的厨师模型中

has_and_belongs_to_many :restuarants

在您的餐厅模型中

has_and_belongs_to_many :chefs