我想定义一个方法(完整),如果用户已经看完课程的所有课程,则该方法返回true。现在,即使查看了所有课程,该方法也只会返回false。
用户模型:
has_many :enrolments, dependent: :destroy
has_many :courses, through: :enrolments
has_many :views
has_many :lessons, through: :views
def enrol(course)
courses << course
end
def enrolled?(course)
courses.include?(course)
end
def view(lesson)
lessons << lesson
end
def viewed?(lesson)
lessons.include?(lesson)
end
def complete(course)
if self.viewed?(course.lessons.all)
return true
else
return false
end
end
课程模型:
belongs_to :course
has_one_attached :file
default_scope -> { order(created_at: :desc) }
has_many :views
has_many :users, through: :views
课程模式:
belongs_to :company
has_many :lessons, dependent: :destroy
default_scope -> { order(created_at: :desc) }
has_many :enrolments, dependent: :destroy
has_many :users, through: :enrolments
查看模型:
belongs_to :user
belongs_to :lesson
答案 0 :(得分:0)
您的viewed?
仅检查一堂课,但您正在其中传递一种关系。
在这种情况下,将数组与课程ID进行比较就足够了:
def complete(course)
lessons.where(course: course).ids.sort == course.lessons.ids.sort
end