From 07640c21640d84624cdb47cb830ea4aa90158ff5 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 26 Apr 2015 11:47:59 -0400 Subject: [PATCH] Attempt to add ordering scope to last_action --- app.rb | 2 +- models/leader.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app.rb b/app.rb index a256a69..02a06b4 100644 --- a/app.rb +++ b/app.rb @@ -12,7 +12,7 @@ get '/' do end leaders = Leader.all.includes(:last_attack, :last_defense) - @top20 = leaders.select { |l| l.last_action }.sort_by { |l| l.last_action } + leaders.reject { |l| l.last_action } + @top20 = leaders.reject { |l| l.last_action } + leaders.select { |l| l.last_action }.sort_by { |l| l.last_action }.reverse erb :index end diff --git a/models/leader.rb b/models/leader.rb index 50517ec..602fb56 100644 --- a/models/leader.rb +++ b/models/leader.rb @@ -1,6 +1,6 @@ class Leader < ActiveRecord::Base - has_one :last_attack, foreign_key: :attacker, primary_key: :leader, class_name: 'Raid' - has_one :last_defense, foreign_key: :defender, primary_key: :leader, class_name: 'Raid' + has_one :last_attack, -> { order created_at: :desc }, foreign_key: :attacker, primary_key: :leader, class_name: 'Raid' + has_one :last_defense, -> { order created_at: :desc }, foreign_key: :defender, primary_key: :leader, class_name: 'Raid' def last_action if last_attack == nil && last_defense == nil