【发布时间】:2015-07-14 07:26:28
【问题描述】:
我已经给出了这个代码
@classes = Class.includes(:student_classes).where(:employee_id => current_employee.id)
@students = {}
unless @classes.blank?
@classes.each { |class|
unless class.student_classes.blank?
class.student_classes.each { |std|
@students[std.student.id] = std.student.name
}
end
}
end
获取@students。请指导我如何干燥这个查询。提前致谢。
【问题讨论】:
-
我想你的类是用
has_many :student_classes声明的。只需添加has_many :students, through: :student_classes并致电Class.where(employee_id: current_employee.id).students。如需更多信息,您可以(并且应该)阅读这篇文章guides.rubyonrails.org/…。 -
是的,我只有这个关联
标签: ruby-on-rails ruby-on-rails-4