0

Currently in my app I have the following models:

  • Student (not connected to the other models yet)
  • Dojo: has_many :training_times
  • TrainingTime: belongs_to :dojo

I want a student to have many training times, and training times to have many students.

Currently in my student controller I have:

def show
  @student = Student.includes(:senseis).find(params[:id])
  @times = TrainingTime.includes(:dojo).order("time ASC")
  @times = sort_by_place_and_day(@times)
end

Which I then render as:

students#show view

If the training time is associated to the current student then I want to highlight it as red, so something like:

<% if time.students.exists?(@student) %>
  <td class="orange">...</td>
<% else %
  <td class="normal">...</td>
<% end % >

So my question is: will this query the database again each time? How do I associate / include the 2 models so that it doesn't?

4

1 回答 1

1

For me, the most efficient approach to this issue is through js. Use erb/haml to show all the training times available then add a js code that will go through each of the training times related to the student. The js code will just change the class of the td which is what you want. So at most, you only get 3 queries, the student, all the training times and the student's training times.

于 2013-02-18T09:58:49.243 回答