Skip to content

Instantly share code, notes, and snippets.

@jeremiahlukus
Last active March 2, 2018 21:10
Show Gist options
  • Save jeremiahlukus/27d57c38417f1533cbbe615f0fd145ee to your computer and use it in GitHub Desktop.
Save jeremiahlukus/27d57c38417f1533cbbe615f0fd145ee to your computer and use it in GitHub Desktop.
FacilityStock.where(product_id: @products).select('MAX(id), facilty_stocks.*').group(:product_id).to_sql
=> "SELECT MAX(id), facilty_stocks.* FROM \"facility_stocks\" WHERE \"facility_stocks\".\"product_id\" IN (SELECT DISTINCT \"products\".\"id\" FROM \"products\" WHERE \"products\".\"company_id\" = 10) GROUP BY \"facility_stocks\".\"product_id\""
FacilityStock.where(product_id: @products).select('MAX(id), facilty_stocks.*').group(:product_id).size
{34=>2, 36=>4, 40=>2}
2.4.1 :054 > sql = "SELECT DISTINCT ON (product_id) * FROM facility_stocks WHERE product_id IN (#{@products.pluck(:id).join(', ')}) ORDER BY product_id, created_at DESC"
2.4.1 :055 > @facility_stocks = ActiveRecord::Base.connection.execute(sql)
2.4.1 :058 > @facility_stocks
=> #<PG::Result:0x007f8a3d033760 status=PGRES_TUPLES_OK ntuples=3 nfields=12 cmd_tuples=3>
2.4.1 :059 > @facility_stocks.each { |n| puts n}
{"id"=>210, "facility_id"=>17, "product_id"=>34, "lot_id"=>nil, "pallet_quantity"=>32, "case_quantity"=>0, "created_at"=>"2018-03-02 02:08:41.048694", "updated_at"=>"2018-03-02 02:08:41.048694", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}
{"id"=>208, "facility_id"=>18, "product_id"=>36, "lot_id"=>nil, "pallet_quantity"=>6, "case_quantity"=>0, "created_at"=>"2018-03-01 21:02:52.911843", "updated_at"=>"2018-03-01 21:02:52.911843", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}
{"id"=>212, "facility_id"=>17, "product_id"=>40, "lot_id"=>nil, "pallet_quantity"=>12, "case_quantity"=>0, "created_at"=>"2018-03-02 02:10:06.496454", "updated_at"=>"2018-03-02 02:10:06.496454", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}
=> #<PG::Result:0x007f8a3d033760 status=PGRES_TUPLES_OK ntuples=3 nfields=12 cmd_tuples=3>
2.4.1 :060 >
2.4.1 :081 > @facility_stocks.select { |k,v| "facility_id, 17"}
=> [{"id"=>210, "facility_id"=>17, "product_id"=>34, "lot_id"=>nil, "pallet_quantity"=>32, "case_quantity"=>0, "created_at"=>"2018-03-02 02:08:41.048694", "updated_at"=>"2018-03-02 02:08:41.048694", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}, {"id"=>208, "facility_id"=>18, "product_id"=>36, "lot_id"=>nil, "pallet_quantity"=>6, "case_quantity"=>0, "created_at"=>"2018-03-01 21:02:52.911843", "updated_at"=>"2018-03-01 21:02:52.911843", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}, {"id"=>212, "facility_id"=>17, "product_id"=>40, "lot_id"=>nil, "pallet_quantity"=>12, "case_quantity"=>0, "created_at"=>"2018-03-02 02:10:06.496454", "updated_at"=>"2018-03-02 02:10:06.496454", "item_quantity"=>0, "reserved_pallet_quantity"=>nil, "reserved_case_quantity"=>nil, "reserved_item_quantity"=>nil}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment