Rails 4 has_many 通过with where子句删除多个关联

Rails 4 has_many through with where clause deletes multiple associations

我有一个项目模型,它为用户提供了各种角色。一个用户可能对一个项目有多个角色。我已经使用 has_many through 和 where 子句设置项目模型关联以显示角色类型:

  has_many :project_user_roles, dependent: :destroy

  has_many :sub_contractors,
           -> { where(project_user_roles: {role: 'SubContractor'}) },
           through: :project_user_roles,
           source: :user

  has_many :consultants,
          -> { where(project_user_roles: {role: 'Consultant'}) },
          through: :project_user_roles,
          source: :user

在我的项目控制器中,我可以更新 sub_contractor_ids 和 consultant_ids。这适用于在所有情况下添加和删除角色,除非用户既是分包商又是顾问,并且顾问角色被删除。在这种情况下,所有角色都被删除了!。传递的参数散列是正确的:

Parameters: {"utf8"=>"✓", "project"=>{"sub_contractor_ids"=>["3", ""], "consultant_ids"=>[""] ...

,但我可以在控制台中看到 update_attributes 命令导致 SQL:

SQL (0.2ms)  DELETE FROM "project_user_roles" WHERE "project_user_roles"."project_id" =  AND "project_user_roles"."user_id" = 3  [["project_id", 3]]

这显然没有说明关联中的 where 子句应该将此删除限制为仅 'Consultant'.

的那些角色

正如我上面提到的,添加角色很好,即如果该用户没有角色并且传递了相同的参数:

Parameters: {"utf8"=>"✓", "project"=>{"sub_contractor_ids"=>["3", ""], "consultant_ids"=>[""] ...

结果 SQL 包括来自 where 子句的角色:

SQL (0.4ms)  INSERT INTO "project_user_roles" ("role", "project_id", "user_id", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id"  [["role", "SubContractor"], ["project_id", 3], ["user_id", 6], ["created_at", "2016-06-07 00:32:17.224502"], ["updated_at", "2016-06-07 00:32:17.224502"]]

我要么没有正确理解 where 子句,要么这不是设置它的最佳方法。非常感谢任何帮助。

虽然我不是 100% 确定为什么这对我来说是一样的,但这就是我现在建立关联的方式:

has_many :consultant_roles,
          -> { where(project_user_roles: {role: 'Consultant'}) },
          class_name: "ProjectUserRole"
has_many :sub_contractor_roles,
          -> { where(project_user_roles: {role: 'SubContractor'}) },
          class_name: "ProjectUserRole"

has_many :main_contractors,
           through: :main_contractor_roles,
           source: :user

has_many :sub_contractors,
           through: :sub_contractor_roles,
           source: :user