hi all
Was wondering how you would handle this situation:
# psuedo code - to illustrate the concept
# not technically correct
* parent has_many: children
# # parents_controller.rb
def index
@parents = Parent.all.includes(:children)
end
<-- parents/index.html.erb -->
<% @parents.each do |parent| %>
<%= parent.name %>
<%= parent.child.name %> <-- How can I get the latest child only, given the above query? -->
<%#= parent.children.last.name %> <-- I could drop the includes statement in the query and fire off an additional query like this in the view, but would prefer not to -->
<% end %>
- As I understand it, if a parent has 500 children, all 500 will be loaded in memory? Many parents may have many children. In this situation though, I would want only the latest child – or perhaps the latest 2 children for each parent – is there any way I can load only a few children in the one query and present them
Am wondering how you folks would handle this type of situation?
Ben
Appreciate your pointers.