Attempt to add ordering scope to last_action

This commit is contained in:
Andrew Tomaka 2015-04-26 11:47:59 -04:00
parent 1bcb9d4bb1
commit 07640c2164
2 changed files with 3 additions and 3 deletions

2
app.rb
View file

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

View file

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