1
我有一个rails 3应用程序。ActiveRecord查询问题
我有以下情况:有学生和老师谁有互动。基于这些相互作用中的一些,教师可以选择给学生奖励,即学生报告。
class Student
has_many :interactions
has_many :teachers, :through=>:interaction
class Teacher
has_many :interactions
has_many :students, :through=>:interaction
has_many :rewards
class Interaction
belongs_to :teacher
belongs_to :student
has_many :student_rewards
has_many :rewards, :through=>:student_reward
class Reward
belongs_to :teacher
has_many :student_rewards
class StudentRewards
belongs_to :reward
belongs_to :interaction
如何将专家码的有效途径,以获取所有学生的教师有很多,并非一定那些学生已经赢得了],并列出对在教师信息的奖励显示?
我试过了,但是我必须在视图中分别获取教师信息,这很糟糕。 (假设student_id数据= 1):
@rewards = Reward.joins(:teacher => :interactions)
.where("interactions.student_id=1")
.paginate(:page => params[:page], :per_page => 5)
问题:
- 这是做的最好的方法是什么?
- 当我在视图中遍历这个时,我必须发出额外的查询来显示关于教师[名字,deomographics]的信息。我怎样才能更有效地获取?
我希望能够做到这一点:
<% for reward in @rewards%>
<%= reward.name, reward.teacher.name, reward.teacher.bio%><br>
<%end%>