Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CreateOrders < ActiveRecord::Migration[5.1]
- def change
- create_table :orders do |t|
- t.timestamps
- end
- end
- end
- class CreateOrderItems < ActiveRecord::Migration[5.1]
- def change
- create_table :order_items do |t|
- t.integer :order_id, index:true
- t.integer :product_id, index:true
- t.integer :amount
- t.timestamps
- end
- end
- end
- class CreateProducts < ActiveRecord::Migration[5.1]
- def change
- create_table :products do |t|
- t.text :name
- t.integer :family_id, index:true
- t.timestamps
- end
- end
- end
- class CreateFamilies < ActiveRecord::Migration[5.1]
- def change
- create_table :families do |t|
- t.text :name
- t.timestamps
- end
- end
- end
- class Family < ApplicationRecord
- has_many :products
- end
- class Order < ApplicationRecord
- has_many :order_items
- has_many :products, through: :order_items
- end
- class OrderItem < ApplicationRecord
- belongs_to :order
- belongs_to :product
- end
- class Product < ApplicationRecord
- belongs_to :family
- has_many :order_items
- end
- irb(main):015:0> Order.joins(:order_items).joins(:products).where("products.family_id":2).where("orders.created_at": [(Time.now).to_date..(Time.now + 1.day).to_date]).count
- (0.6ms) SELECT COUNT(*) FROM "orders" INNER JOIN "order_items" ON "order_items"."order_id" = "orders"."id" INNER JOIN "order_items" "order_items_orders_join" ON "order_items_orders_join"."order_id" = "orders"."id" INNER JOIN "products" ON "products"."id" = "order_items_orders_join"."product_id" WHERE "products"."family_id" = ? AND ("orders"."created_at" BETWEEN '2018-04-20' AND '2018-04-21') [["family_id", 2]]
- => 3
Add Comment
Please, Sign In to add comment