On Rails 4. I am trying to do a complex nested each loop with Bootstrap.
I have the models Users, Organizations, and Submissions. Submissions belongs to Users and Organizations--the current user creates new submissions and assigns it to an org (so, Submissions has the attributes :user_id
and :organization_id
).
I made a little image to illustrate what I am trying to do in the submissions index view:
Listing all of current user's submissions:
Basically, I need to find each organization/year the current user has assigned a submission to, then within that heading, group each current user's submission that belongs to the same org/year.
In order to get this formatting with Bootstrap, I need to have a .in_groups_of(3, false)
statement (unless someone can tell me about something different) with a <div class="col-md-4">
layout. Here is my current code in the submission index view, which is not working as expected:
# find all submissions made by the current user, group by the
# submission's :contest_year and :organization_id attributes:
<% current_user.submissions.group([:contest_year, :organization_id]).each do |contest| %>
<h2><%= contest.contest_year %>: <%= contest.organization.name %></h2>
# here is where it fails, it is finding all the submissions made by the user
# instead of just the submissions that belong under the
# specified contest_year/organization_id heading from above.
# I know I need 'contest' involved in here
<% current_user.submissions.in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |submission| %>
<div class="col-md-4">
# submission information here
</div>
<% end %>
</div><br/>
<% end %>
<% end %>
This code returns something like this instead:
I hope all of this makes sense. So, how do I fix the nested loop so that it behaves like the first image. Thanks very much. Partial submission model/controller:
Submission.rb
belongs_to :user
belongs_to :organization
submissions_controller.rb
def index
@submissions = Submission.all
end