I have 3 Models.
ProjectDate (available dates for a project) which
has_many Events
Events (the appointments on any given ProjectDate)
belongs_to ProjectDate
belongs_to User
User (the person who created the Event)
has_many Events
I am wondering how I can use includes (or if I can use includes) to fetch the User attributes associated with an Event when I'm also fetching all the Events associated with a ProjectDate.
I want to be able to pull the user.firstname (see view code below)
This is how I fetch an array of ProjectDats with their associated Events:
@projectschedule = ProjectDate.where(project_id: params[:p]).includes(:events).order(the_date: :asc)
I am really stuck.
Here is the view code:
<table width="100%;" class="">
<tr>
<td >
<%=p.the_date.strftime("%A")%><br>
<%=p.the_date.strftime("%b. %d ")%>
</td>
<td class="dateinfo"><% p.events.users.each do |e| %>
<ul >
<li ><%= e.starts_on.strftime("%l:%m %P ") %><%= e.user.firstname %><%= e.description %><button >O</button><button >O</button></li>
</ul><% end %>
</td>
</tr>
</table>
<% end %>
You can nest include arguments like this.
@projectschedule = ProjectDate.where(project_id: params[:p]).includes(events: :user).order(the_date: :asc)
Note events: :user
, which will make sure both events and it's user are eager loaded.