设置用户之间的关系以访问资源
Setting a relationship between Users to access a resource
我正在自学 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
员工 table 也有一个 user_type 列来处理项目(餐厅)内的权限。我不知道如何让我的 employee_controller 设置这种关系。用户主键是 :email 所以我猜一个表单应该能够接收 :email 参数,检查是否存在具有输入电子邮件的用户,并将关系添加到员工 table.
我希望能够做这样的事情:
Restaurant_A = Restaurant.create(restaurant_params)
User_A.restaurants = Restaurant_A
Restaurant_A.employees = User_B
我认为我的模型可能是错误的,但本质上我希望能够让用户能够创建一家餐厅,并被添加为另一家 restaurant/their 自有餐厅的员工。
您的模型没问题 - 没问题。
您想要完成的事情,您可以通过以下方式完成:
restaurant_a = Restaurant.create(restaurant_params)
# Remember to name it 'restaurant_a', it is convention in Ruby
user_a.restaurants << restaurant_a
<<
是一个运算符,它把左边的东西插入到右边的东西里。所以在我们的例子中,它会将 restaurant_a
插入到与 user_a
相关联的 restaurants
列表中,然后你在 user_a
上调用 save
操作,例如user_a.save
.
同案在另一边:
restaurant_a.employees << user_b
# According to Ruby convention, you shouldn't start your variable
# name with an upper case letter, and you should user a convention
# called 'snake_type' naming convention. So instead of naming
# your variable like 'firstDifferentUser', name it 'first_different_user'
# instead.
restaurant_a.save # To successfully save the record in db
编辑:
用于创建表单:
<%= form_for(@restaurant, @employee) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<% end %>
并且您需要在员工的控制器 new
操作中定义 @restaurant
和 @employee
,因为您要为特定餐厅创建新员工。
我正在自学 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
员工 table 也有一个 user_type 列来处理项目(餐厅)内的权限。我不知道如何让我的 employee_controller 设置这种关系。用户主键是 :email 所以我猜一个表单应该能够接收 :email 参数,检查是否存在具有输入电子邮件的用户,并将关系添加到员工 table.
我希望能够做这样的事情:
Restaurant_A = Restaurant.create(restaurant_params)
User_A.restaurants = Restaurant_A
Restaurant_A.employees = User_B
我认为我的模型可能是错误的,但本质上我希望能够让用户能够创建一家餐厅,并被添加为另一家 restaurant/their 自有餐厅的员工。
您的模型没问题 - 没问题。
您想要完成的事情,您可以通过以下方式完成:
restaurant_a = Restaurant.create(restaurant_params)
# Remember to name it 'restaurant_a', it is convention in Ruby
user_a.restaurants << restaurant_a
<<
是一个运算符,它把左边的东西插入到右边的东西里。所以在我们的例子中,它会将 restaurant_a
插入到与 user_a
相关联的 restaurants
列表中,然后你在 user_a
上调用 save
操作,例如user_a.save
.
同案在另一边:
restaurant_a.employees << user_b
# According to Ruby convention, you shouldn't start your variable
# name with an upper case letter, and you should user a convention
# called 'snake_type' naming convention. So instead of naming
# your variable like 'firstDifferentUser', name it 'first_different_user'
# instead.
restaurant_a.save # To successfully save the record in db
编辑:
用于创建表单:
<%= form_for(@restaurant, @employee) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<% end %>
并且您需要在员工的控制器 new
操作中定义 @restaurant
和 @employee
,因为您要为特定餐厅创建新员工。