From 744f8090ba68a68e69ceb47a28677b3297fba209 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Wed, 10 Dec 2014 10:09:53 +0100 Subject: [PATCH] Add orders_count and ordering on overview --- app/controllers/orders_controller.rb | 2 +- app/models/order.rb | 2 +- db/migrate/20141210090544_add_orders_count_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20141210090544_add_orders_count_to_users.rb diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index f670759..df2ce2a 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -27,7 +27,7 @@ class OrdersController < ApplicationController def index @users_by_name = User.all.order(:name) - @users_by_order = User.all + @users_by_order = User.all.order(:orders_count).reverse_order end private diff --git a/app/models/order.rb b/app/models/order.rb index 9307b9e..6b30597 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -11,7 +11,7 @@ class Order < ActiveRecord::Base after_initialize { self.total_price = 0 } - belongs_to :user + belongs_to :user, counter_cache: true has_many :order_products has_many :products, { through: :order_products} do diff --git a/db/migrate/20141210090544_add_orders_count_to_users.rb b/db/migrate/20141210090544_add_orders_count_to_users.rb new file mode 100644 index 0000000..c84a940 --- /dev/null +++ b/db/migrate/20141210090544_add_orders_count_to_users.rb @@ -0,0 +1,5 @@ +class AddOrdersCountToUsers < ActiveRecord::Migration + def change + add_column :users, :orders_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index f1ba18d..6cd9b8b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141209204351) do +ActiveRecord::Schema.define(version: 20141210090544) do create_table "order_products", force: true do |t| t.integer "order_id" @@ -60,6 +60,7 @@ ActiveRecord::Schema.define(version: 20141209204351) do t.string "avatar_content_type" t.integer "avatar_file_size" t.datetime "avatar_updated_at" + t.integer "orders_count", default: 0 end end