mardi 16 mai 2017

How to includes or join into with a each method?

I'm discovering the methods join & includes with rails. Which are great, but I'm not really fluent with it, so I need a little advice about it.

I explain my situation, I'm displaying a list of groups into the index view. Each of them have a modal associated. And into this modal, I would like to display a list of the requests associated to this group. So normally, I user the fonction [ @requests = group.requests ]. But I would like to use join for that for sending just one request to my sql.

Because i'm in the index view, I don't have a @group into my action. So how I can do that ?

My code :

controller :

def index
  @groups = current_user.groups
end 

view (index) :

<% @groups.each do |g| %>
  <MODAL>
    <% @requests = g.requests %>
    <% @requests.each do |r| %>
      <%= r.date %>
    <% end %>
  </MODAL>
<% end %>

I guess I can also use join & include for @groups, but there is already just one sql request, so I'm good with it.

Aucun commentaire:

Enregistrer un commentaire