【发布时间】:2015-07-10 20:31:54
【问题描述】:
我正在自学 Rails,我正在尝试建立一种协作关系,就像 Github 将协作者添加到项目中一样。我的模型如下所示:
class Restaurant < ActiveRecord::Base
has_many :employees
has_many :users, through: :employees
end
class User < ActiveRecord::Base
has_many :employees
has_many :restaurants, through: :employees
end
class Employee < ActiveRecord::Base
belongs_to :restaurant
belongs_to :user
end
employee 表还有一个 user_type 列来处理项目(餐厅)内的权限。我不知道如何让我的employee_controller 设置这种关系。用户主键是 :email 所以我猜一个表单应该能够接收 :email 参数,检查输入电子邮件的用户是否存在,并将关系添加到员工表中。
我希望能够做这样的事情:
Restaurant_A = Restaurant.create(restaurant_params)
User_A.restaurants = Restaurant_A
Restaurant_A.employees = User_B
我认为我的模型可能是错误的,但本质上我希望能够让用户能够创建餐厅以及被添加为另一家餐厅/他们自己的餐厅的员工。
【问题讨论】:
标签: ruby-on-rails many-to-many has-and-belongs-to-many