I'm having some trouble defining a has_many :through relationship in Rails 5 where the join and target models both reside within a namespace. I have a Student
model that I want to join to Admission::Application
through Admission::ApplicationStudent
. When I attempt to access the admission_applications
association I get a NameError: "uninitialized constant Student::Application".
我在定义一个has_many:在Rails 5中的关系时遇到一些麻烦,其中连接和目标模型都位于命名空间内。我有一个学生模型,我想通过Admission :: ApplicationStudent加入Admission :: Application。当我尝试访问admission_applications关联时,我得到一个NameError:“未初始化的常量Student :: Application”。
Here are my model definitions:
以下是我的模型定义:
student.rb
student.rb
class Student < ApplicationRecord
has_many :admission_application_students,
class_name: 'Admission::ApplicationStudent',
inverse_of: :student
has_many :admission_applications,
through: :admission_application_students,
source: :application
end
admission/application_student.rb
入院/ application_student.rb
class Admission::ApplicationStudent < ApplicationRecord
belongs_to :application
belongs_to :student,
class_name: 'Student',
inverse_of: :admission_application_students
end
admission/application.rb
入院/ application.rb中
class Admission::Application < ApplicationRecord
has_many :application_students
has_many :students,
through: :application_students,
source: :student
end
Incidentally, I generated all three models with standard commands:
顺便说一句,我使用标准命令生成了所有三个模型:
rails g model student
rails g model 'Admission::Application'
rails g model 'Admission::ApplicationStudent'
The application_students
and students
associations are both functioning correctly from Application
. I can also retrieve a collection of ApplicationStudent
records from the admission_application_students
association on Student
. If Student
knows how to make it that far, it seems like :source
has to be the problem, but I've been researching and testing since yesterday, and I no longer feel like I'm making progress. Any help would be appreciated.
application_students和学生关联都在Application中正常运行。我还可以从Student上的admission_application_students关联中检索ApplicationStudent记录的集合。如果学生知道如何做到这一点,似乎:来源必须是问题,但我从昨天起一直在研究和测试,我不再觉得我在取得进展。任何帮助,将不胜感激。
1 个解决方案
#1
1
You need to specify the class_name
for admission_applications
association since it is also in a different namespace:
您需要为admission_applications关联指定class_name,因为它也位于不同的命名空间中:
class Student < ApplicationRecord
has_many :admission_application_students, class_name: 'Admission::ApplicationStudent'
has_many :admission_applications,
through: :admission_application_students,
source: :application,
class_name: 'Admission::Application'
end
For more information see the documentation for has_many and Section 3.4 Controlling Association Scope of the Active Record Associations Guide.
有关更多信息,请参阅has_many文档和“3.4活动记录关联指南控制关联范围”。
#1
1
You need to specify the class_name
for admission_applications
association since it is also in a different namespace:
您需要为admission_applications关联指定class_name,因为它也位于不同的命名空间中:
class Student < ApplicationRecord
has_many :admission_application_students, class_name: 'Admission::ApplicationStudent'
has_many :admission_applications,
through: :admission_application_students,
source: :application,
class_name: 'Admission::Application'
end
For more information see the documentation for has_many and Section 3.4 Controlling Association Scope of the Active Record Associations Guide.
有关更多信息,请参阅has_many文档和“3.4活动记录关联指南控制关联范围”。