Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeremiahlukus/040a9b40be1d7822d2988e2c10fd50f9 to your computer and use it in GitHub Desktop.
Save jeremiahlukus/040a9b40be1d7822d2988e2c10fd50f9 to your computer and use it in GitHub Desktop.
@products = Product.by_company(current_user.company.slug).includes(:facilities, :total_stocks).distinct(:sku)
sql = "SELECT DISTINCT ON (product_id) * FROM facility_stocks WHERE product_id IN (#{@products.pluck(:id).join(', ')}) ORDER BY product_id, created_at DESC"
@facility_stocks = ActiveRecord::Base.connection.execute(sql)
in view
<td><%= @facility_stocks.select { |h| h['facility_id' ] == facility.id}.map { |h| h['pallet_quantity'] }.sum %></td>
<td><%= @facility_stocks.select { |h| h['facility_id' ] == facility.id}.map { |h| h['case_quantity'] }.sum %></td>
<td><%= @facility_stocks.select { |h| h['facility_id' ] == facility.id}.map { |h| h['item_quantity'] }.sum %></td>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment