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:
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?