【发布时间】:2023-03-15 21:30:01
【问题描述】:
我正在尝试检查学生是否尝试了指定的测试。我想链接相关模型以将查询数量减少到只有 1。以下是我的模型:
class Test < ActiveRecord::Base
has_many :assigns
has_many :attempts
belongs_to :topic
end
class Topic < ActiveRecord::Base
has_many :tests
has_many :attempts
has_many :assigns, through: :test
end
class Assign < ActiveRecord::Base
belongs_to :test
belongs_to :student
has_many :attempts
end
class Attempt < ActiveRecord::Base
belongs_to :test
belongs_to :topic
belongs_to :assign
belongs_to :student
end
我想检查特定学生(id:100)是否尝试了指定的测试,并检索其他详细信息,例如测试的主题名称。到目前为止,我有这样的事情:
ta = Assign.includes(:test => {:topic => :attempts})
这允许我在单个查询中检索详细信息,例如 test_name、topic_name、何时分配等。如何在同一查询中还包括 student_id: 100 的尝试记录?就我现在所拥有的,当我检索学生的尝试详细信息时,正在生成一个全新的查询。
我想要的是类似以下的东西,而不必再次接触数据库:
ta.test.attempts.where(student_id: 100)
如何只用一个查询完成所有这些操作?
【问题讨论】:
-
是
test_前缀是assign、topic和attempt所必需的吗?这里发生的事情太多了,有点难以阅读。你能放一张ER图吗? -
@lusketeer 为清楚起见删除了 test_。
标签: sql ruby-on-rails activerecord