My table named stocks contains product_id, color_id, storage_id and in_stock
as columns.
For a given product I want to group all stocks by storage, then for each storage I want to show product (color): in_stock
How should I write a method and how to render?
<% Stock.all.group_by(&:storage).each do |storage, products| %>
Storage: <%= storage %>
<% products.each do |product| %>
(<%= product.color_id %>): <%= product.in_stock %>
<% end %>
<% end %>
Edit: updated to use ERB, and in_stock as a number and not a boolean yes/no column, and use color_id in place of color.name because that assumes you have a relationship to a color.
Not structured exactly how you need it (not enough info from your post), but something like this perhaps?
items = Stock.select('color_id, in_stock').group('stocks.storage_id').where(:product_id => foo)
items.each {|item| puts "(#{lookup_color(item.color_id)}): #{in_stock}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With