ruby-on-rails - 在单个继承表和另一个表中的类之间建立多对多关联

标签 ruby-on-rails join activerecord associations single-table-inheritance

在部门和研究人员之间的多对多关系中,我有一个名为“关系”的联接表。

我希望能够通过执行 Department.find(1).students 来获取学生列表但我得到ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s) :students in model Researcher. Try 'has_many :students, :through => :researchers, :source => <name>'.)

为什么它不使用 Researcher 表中的范围?

class Department < ApplicationRecord
  has_many :relations
  has_many :researchers, through: :relations
  has_many :students, source: :students, through: :researchers
  has_many :advisors, source: :advisors, through: :researchers
end

class Relation < ApplicationRecord
  belongs_to :researcher
  belongs_to :department
end

class Reseacher < ApplicationRecord
  scope :students, -> { where(type: 'Student') }
  scope :advisors, -> { where(type: 'Advisor') }
end

class Student < Researcher
  has_many :relations, foreign_key: :department_id
  has_many :departments, through: :relations
end

class Advisor < Researcher
  has_many :relations, foreign_key: :department_id
  has_many :departments, through: :relations
end

最佳答案

source: 选项需要关联作为参数。内部, rails runs对论点的反射(reflection),例如:

# source: :students, through: :researchers
>> Researcher.reflect_on_association(:students)
=> nil

在修复 has_many :students 关联之前,需要注意以下几点:

has_many :students,     # will look for `students` association in the intermediate
                        # model unless source is specified; intermediate model is
                        # determined by reflecting on through option `:researchers`
                        #
                        #   reflect_on_association(:researchers).klass # => Researcher

  through: :researchers # can't go through `researchers`; already there.
                        # `Student` is a `Researcher`.

  source: :students,    # there is no `students` association in `Researcher` class.
                        #
                        #   reflect_on_association(:researchers).klass
                        #     .reflect_on_association(:students) # => nil

要修复关联,我们可以使用 has_many 方法的 scope 参数:

has_many(name, scope = nil, **options, &extension)
#              ^ pass a proc as a second argument
class Department < ApplicationRecord
  # NOTE: add `dependent: :destroy` option to destroy corresponding Relations
  #       when destroying a Department 
  has_many :relations, dependent: :destroy
  has_many :researchers, through: :relations

  has_many :students, 
    -> { where(type: "Student") }, # scope the associated model

    through: :relations,           # relevant association is in Relation model

    source:  :researcher           # look for `researcher` association in Relation.
                                   # instead of `student`

  # NOTE: use existing scope from another model
  has_many :advisors,
    -> { advisors },               # this runs in the source class.
    through: :relations,           #                    |
    source:  :researcher           # <------------------'
                                   # Researcher has `advisors` class method,
                                   # defined by `scope: :advisors`.
end

现在,我们需要修复 RelationResearcher 之间的关联:

# NOTE: what if you need another "relation" class to make another many-to-many association.
# TODO: call this something a bit more descriptive like `DepartmentStaff`
#       or use the conventional `DepartmentResearcher`.
class Relation < ApplicationRecord
  belongs_to :researcher
  belongs_to :department
end

class Researcher < ApplicationRecord
  scope :students, -> { where(type: "Student") }
  scope :advisors, -> { where(type: "Advisor") }

  # NOTE: `has_many :relations` is the opposite of `belongs_to :researcher`
  #       `foreign_key` is `researcher_id` which is the default and
  #       should not be changed.
  # has_many :relations, foreign_key: :department_id

  has_many :relations, dependent: :destroy        # <--.
  has_many :departments, through: :relations      # <--|
end                                               #    |
                                                  #    |
class Student < Researcher                        #    |
  # NOTE: no need to duplicate these; put it in the parent class.
  # has_many :relations
  # has_many :departments, through: :relations
end

class Advisor < Researcher
end
>> Relation.create!([{researcher: Student.new, department: Department.create},{researcher: Advisor.new, department: Department.first}])

>> Department.first.students
=> [#<Student:0x00007f7f78ae5f98 id: 1, type: "Student">]

>> Department.first.advisors        
=> [#<Advisor:0x00007f7f789a9b20 id: 2, type: "Advisor">]

>> Department.first.researchers                                                            
=> [#<Student:0x00007f7f786bdc90 id: 1, type: "Student">, #<Advisor:0x00007f7f786bd858 id: 2, type: "Advisor">]

您还可以通过在 Relation 中定义其他关联来让 Rails 完成工作,无需范围:

class Relation < ApplicationRecord
  belongs_to :researcher
  belongs_to :department

  belongs_to :student, foreign_key: :researcher_id, optional: true
  belongs_to :advisor, foreign_key: :researcher_id, optional: true
end

class Department < ApplicationRecord
  has_many :relations, dependent: :destroy
  has_many :researchers, through: :relations

  has_many :students, through: :relations
  has_many :advisors, through: :relations
end

https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

https://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope

https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

https://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_association

关于ruby-on-rails - 在单个继承表和另一个表中的类之间建立多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72372726/

相关文章:

ruby-on-rails - Heroku Postgres : Make existing database follower

mysql - 统计每个部门的所有连接用户

java - JPA中使用中间表列进行排序

ruby-on-rails - 单行 has_many_through 创建命令

ruby-on-rails - 有没有办法删除 Sublime Text 3 中的所有注释行?

ruby-on-rails - 设计通过电子邮件或手机号码注册

mysql - SQL:如何对表变量添加限制?

ruby-on-rails - 查找日期范围内的记录

mysql - 无法使用 ActiveRecord 更新属性

ruby-on-rails - Ruby on Rails - 具有部分 View 的实例变量的范围