多目标has_many通过:

时间:2018-10-12 20:37:49

标签: ruby-on-rails database activerecord

我有三种型号:

User(name: string)
Course(name: string)
Assoc(user_id: integer, ta_id: integer, teach_id: integer, student_id: integer)

我希望能够将用户与课程相关联,例如ta,老师或学生。我不确定这与ActiveRecord中的:polymorphic想法是否匹配。这是我到目前为止的内容,但效果不佳:

class User < ApplicationRecord
  has_many :tas, class_name: "Assoc", foreign_key: :ta_id
  has_many :teaches, class_name: "Assoc", foreign_key: :teach_id
  has_many :takes, class_name: "Assoc", foreign_key: :student_id

  has_many :ta_courses, through: :tas
  has_many :taken_courses, through: :tas
  has_many :taught_courses, through: :tas
end

它不起作用:

irb(main):056:0> User.find(1).ta_courses
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
Traceback (most recent call last):
        1: from (irb):56
ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s) "ta_course" or :ta_courses in model Assoc. Try 'has_many :ta_courses, :through => :tas, :source => <name>'. Is it one of ?)
irb(main):057:0>

任何指针将不胜感激!

2 个答案:

答案 0 :(得分:1)

您似乎尚未建立从Assoc模型到Course模型的关系。您的Assoc模型应该具有一个course_id,因此它看起来像:

Assoc(user_id: integer, ta_id: integer, teach_id: integer, student_id: integer, course_id: integer)

然后您将需要在Assoc模型上建立一个Emirates_to关系:

class Assoc < ApplicationRecord
  belongs_to :course
end

最后,用户模型中的has_many通过关系的构建不正确(全部通过tas)。您的错误消息为您提供了有关您需要做的最后一件事的线索,那就是使用source标识别名关系:

class User < ApplicationRecord
  has_many :tas, class_name: "Assoc", foreign_key: :ta_id
  has_many :teaches, class_name: "Assoc", foreign_key: :teach_id
  has_many :takes, class_name: "Assoc", foreign_key: :student_id

  has_many :ta_courses, through: :tas, source: :course
  has_many :taught_courses, through: :teaches, source: :course
  has_many :taken_courses, through: :takes, source: :course
end

答案 1 :(得分:1)

如果我真的必须能够查询所有用户->课程关系作为一个表,则可以这样设置:

# rails g model assoc user:belongs_to course:belongs_to
class Assoc < ApplicationRecord
  enum role: [:student, :teacher, :assistent]
  belongs_to :user
  belongs_to :course
end

然后,我们要在用户和课程中为每个角色设置范围关联:

has_many :student_assocs, -> { where(role: :student) }
  class_name: 'Assoc'

由于我们都希望两者具有完全相同的关联,所以我们可以使用模块将其保持为干态:

app/models/concerns/associated.rb
# dynamically creates assocations for each role in Assoc.roles enum
module Associated
  extend ActiveSupport::Concern
  included do
    # the plain base assocation
    has_many :assocs
    # this creates the assocations student_assocs, teacher_assocs, etc
    Assoc.roles.keys.each do |role|
      # We need to use eval for the second argument as we are creating the lambda dynamically
      has_many :"#{role}_assocs", eval( "->{ where(role: #{Assoc.roles[role]})}" ),
        class_name: 'Assoc'
    end
  end
end

Assoc.roles给出了我们在Assoc中设置的枚举映射的哈希值。

然后我们可以在课程和用户中包含我们的模块,并设置间接关联:

class Course < ApplicationRecord
  include Associated
  has_many :users, through: :assocs
  # this creates the assocations students, teachers, etc
  Assoc.roles.keys.each do |role|
    has_many role.pluralize.to_sym,
      through: "#{role}_assocs".to_sym,
      source: :user
  end
end

class User < ApplicationRecord
  include Associated
  has_many :courses, through: :assocs
   # this creates the assocations courses_as_student, courses_as_teacher, etc
  Assoc.roles.keys.each do |role|
    has_many "course_as_#{role}".to_sym,
      through: "#{role}_assocs".to_sym,
      source: :course
  end
end