Skip to content

Instantly share code, notes, and snippets.

@alcidesjunior
Created November 23, 2015 13:44
Show Gist options
  • Save alcidesjunior/ca82717e4b40c4c17df7 to your computer and use it in GitHub Desktop.
Save alcidesjunior/ca82717e4b40c4c17df7 to your computer and use it in GitHub Desktop.
Problema com estrutura
Order.where(:created_at=>dt1.beginning_of_day..dt2.end_of_day).each{|p1|
dt << p1.created_at.strftime("%d/%m/%Y")
Orderitem.where(orders_id: p1.id).each {|p2|
quantity << {data: p1.created_at.strftime("%d/%m/%Y"), product: Product.find_by_id(p2.products_id).product_name,quantity: p2.quantity}
}
}
@fidelisrafael
Copy link

OrderItem
  .joins(:products)
  .joins(:orders)
  .select("products.product_name, order_ids, COUNT(*) as quantity, date_trunc(day, date) as date_interval")
  .group_by(:orders_ids)
  .where(orders: { created_at: (dt1.beginning_of_day..dt2.end_of_day) })

@alcidesjunior
Copy link
Author

Sobre a estrutura:
============Order==========
t.integer :ordertype
t.integer :desk, defalt: 0
t.integer :status, default: 0 # 0 aberto 1 pago 2 cancelado
t.float :total
t.float :subtotal
t.float :special_rate, default:true
t.integer :paymenttype
t.float :cash
t.float :transshipment, default:0
t.references :clients, index: true
t.references :desks, index: true
t.references :cash_registers, index: true
t.timestamps null: false

===========Orderitem=====================
t.belongs_to :products, index: true
t.belongs_to :orders, index: true
t.integer :quantity
t.timestamps null: false

==========Products==========
t.string :product_name
t.string :product_code
t.float :price
t.integer :quantity, default: 0
t.integer :minquantity,default: 4
t.integer :category_id
t.boolean :count
t.references :category, index: true
t.timestamps null: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment