如何链接多个模型以减少SQL查询的数量

时间:2017-05-16 04:02:38

标签: sql ruby-on-rails activerecord

我试图检查学生是否尝试过指定的考试。我想链接相关模型以将查询数量减少到只有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

我想检查特定学生(身份:100)是否尝试过指定的测试,并检索其他详细信息,例如测试的主题名称。到目前为止,我有这样的事情:

ta = Assign.includes(:test => {:topic => :attempts})

这允许我在单个查询中检索详细信息,例如test_name,topic_name,分配时等。如何在同一查询中包含student_id:100的尝试记录?凭借我现在所拥有的,当我检索学生的尝试细节时,正在生成一个全新的查询。

我想要的是像下面这样的东西,而不必再次触摸数据库:

ta.test.attempts.where(student_id: 100)

如何通过一个查询完成所有这些操作?

1 个答案:

答案 0 :(得分:0)

好的,既然你想要所有联合表中的各种信息,那么你必须从头开始加入它们。

Attempt.joins(:topic, :test, :assign)

然后您可以使用student_id

过滤它

.where("attempts.student_id" => 100)

最后,你想要的字段

.select("attempts.id as attempt_id, tests.name as test_name, topics.name as topic_name, assigns.created_at as assigned_at")

总结

Attempt.joins(:topic, :test, :assign).where("attempts.student_id" => 100).select("attempts.id as attempt_id, tests.name as test_name, topics.name as topic_name, assigns.created_at as assigned_at")

相关问题