trugul-bot/models/leader.rb

19 lines
694 B
Ruby
Raw Normal View History

2015-04-25 13:17:47 -04:00
class Leader < ActiveRecord::Base
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'
2015-04-25 13:17:47 -04:00
2015-04-26 11:42:19 -04:00
def last_action
if last_attack == nil && last_defense == nil
@last_action = nil
elsif last_defense == nil
@last_action = last_attack.created_at
elsif last_attack == nil
@last_action = last_defense.created_at
else
@last_action = (last_attack.created_at > last_defense.created_at) ? last_attack.created_at : last_defense.created_at
end
return @last_action
end
2015-04-25 13:17:47 -04:00
end