Rails ActiveRecord has_many:通过多态关联

时间:2013-11-14 19:49:56

标签: ruby-on-rails ruby activerecord

我有这些模特:

class User < ActiveRecord::Base
  has_many :managements, class_name: 'Management', foreign_key: :manager_id
  has_many :primary_locations, -> { where managements:
    { manager_role: "primary" } }, through: :managements,
    source: :manageable, source_type: 'Location'
  has_many :secondary_locations, -> { where managements:
    { manager_role: "secondary" } }, through: :managements,
    source: :manageable, source_type: 'Location'
  has_many :primary_departments, -> { where managements:
    { manager_role: "primary" } }, through: :managements,
    source: :manageable, source_type: 'Department'
  has_many :secondary_departments, -> { where managements:
    { manager_role: "secondary" } }, through: :managements,
    source: :manageable, source_type: 'Department'
end

用户可以管理多个部门和位置,并且可以是每个部门和主要经理:

class Management < ActiveRecord::Base
  belongs_to :manager, class_name: 'User', foreign_key: :manager_id
  belongs_to :manageable, polymorphic: true
end

class Location < ActiveRecord::Base
  has_many :managements, class_name: "Management", foreign_key: :manageable_id,
    as: :manageable
  has_many :primary_managers, -> { where management:
    { manager_role: "primary" } }, through: :managements, source: :manager
  has_many :secondary_managers, -> { where management:
    {manager_role: "secondary"} }, through: :managements, source: :manager
end

class Department < ActiveRecord::Base
  has_many :managements, class_name: "Management", foreign_key: :manageable_id,
    as: :manageable
  has_many :primary_managers, -> { where management:
    { manager_role: "primary" } }, through: :managements, source: :manager
  has_many :secondary_managers, -> { where management:
    {manager_role: "secondary"} }, through: :managements, source: :manager
end

当我致电user.primary_departments时,以下关联会产生错误:

SELECT "departments".* FROM "departments" INNER JOIN "managements" ON
  "departments"."id" = "managements"."manageable_id" WHERE
  "managements"."manager_role" = 'primary' AND "managements"."manager_id" = $1
   AND "departments"."manageable_type" = 'Department'  [["manager_id", 1]]
PG::UndefinedColumn: ERROR:  column department.manageable_type does not exist

location.primary_managers协会有效。所以它似乎几乎可以工作,除了多态类型指向错误的表。如何指定在manageable_type表中找到management

1 个答案:

答案 0 :(得分:0)

我最后将其添加到User类:

%w(department location).each do |type|
  %w(primary secondary).each do |role|
    define_method("#{role}_managed_#{type}s") do
      klass = type.capitalize.constantize
      klass.joins("#{role}_managers".to_sym).where('manager_id = ?', id)
    end
  end
end

它添加了4种方法:primary_managed_departmentssecondary_managed_departmentsprimary_managed_locationssecondary_managed_locations。这是一种适合我们需求的解决方法,但可能还有其他我不想要的东西。我将这个答案主要留给@ janko-m,因为我无法将格式化的代码放在评论中,所以请随意回答更好的解决方案。