【发布时间】:2017-10-24 10:06:32
【问题描述】:
我一直在阅读这篇文章,但无法将其写入 Rails 范围:
find all parent records where all child records have a given value (but not just some child records)
我有一个 Course、Section 和 Quiz 对象:
class Course < ActiveRecord::Base
has_many :course_members
has_many :members, through: :course_members
has_many :sections
has_many :quizzes, through: :sections
end
class Quiz < ActiveRecord::Base
belongs_to :member
belongs_to :section
end
class Section < ActiveRecord::Base
belongs_to :course
has_many :quizzes
end
我想查找会员的所有课程,其中与该课程相关的所有测验都具有completed = true 属性。
所以在我的Member 课程中,我最好写一些类似的东西:
has_many :completed_courses, -> {
joins(:courses, :quizzes, :sections)
# .select( 'CASE WHEN quizzes.completed = true then 1 end') ??? maybe ???
}, class_name: 'Course'
哈哈!但除非这太复杂了。我一直在尝试把这个简单地写在Course 中也可以。
【问题讨论】:
-
为什么是
relational-algebra?
标签: sql ruby-on-rails postgresql activerecord relational-algebra