Add orders_count and ordering on overview

This commit is contained in:
Benjamin Cousaert 2014-12-10 10:09:53 +01:00
parent f6a37a80cf
commit 744f8090ba
4 changed files with 9 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class AddOrdersCountToUsers < ActiveRecord::Migration
def change
add_column :users, :orders_count, :integer, default: 0
end
end

View file

@ -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