The title may not have described my question properly, I wasn't sure how to explain what I'm doing without visuals.
I have a set of snags, each snag is connected to a plot (a plot has many snags). Each plot is linked to a development (a development has many plots). Each development is linked to a user (a user has many developments).
I want to return a list of snags that are relevant to each developer.
I currently have the following in my snags controller:
def index
@developments = Development.accessible_by(current_ability)
.where(enable_snagging: true)
@developments.each do |development|
@plots = development.plots
get_snags(@plots)
end
end
private
def get_snags(plots)
plots.each do |plot|
@plot_snags = Snag.where(plot_id: plot.id)
end
end
And in my view:
<table class="record-list snags">
<thead>
<tr>
<th><%= sortable(@snags, :id) %></th>
</tr>
</thead>
<tbody>
<% @snags.each do |snag| %>
<tr data-snag="<%= snag.id %>">
<td><%= snag.id %></td>
</tr>
<% end %>
</tbody>
</table>
I initially had @snags in place of @plot_snags in the controller, but of course that only returned the last snag / set of snags, rather than all of them.
If I try to create and return a @snags array and add each object in @plot_snags to it (or any other variation of declaring @snags as an array), I get:
undefined method `human_attribute_name' for #<Array:0x007ff5334861d0>
I can't figure out what I'm doing wrong here.
UPDATE
Having looked at it with fresh eyes again today - I couldn't figure out why my join table isn't populating, I will have a look into it again today to get things working 'properly' - it turns out the error I was getting was due to the Sortable interface I was trying to implement in the view. Once I removed the Sortable I was able to populate my snags array and I get the output I was hoping for.