Sha256: 4c65062252fc057307de1e911f53f98812721611133e9f357dec220632ae4070

Contents?: true

Size: 960 Bytes

Versions: 26

Compression:

Stored size: 960 Bytes

Contents

# == Schema Information
#
# Table name: lentil_battles
#
#  id         :integer          not null, primary key
#  image_id   :integer
#  loser_id   :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Lentil::Battle < ActiveRecord::Base
  attr_accessible :image_id, :loser_id, :loser

  belongs_to :image, :counter_cache => :wins_count, :autosave => true
  belongs_to :loser, :class_name => "Image", :counter_cache => :losses_count, :autosave => true

  after_commit :update_win_pct

  protected
  def update_win_pct
    self.image.reload
    self.loser.reload

    winner_pct = (self.image.wins_count.to_f / (self.image.wins_count + self.image.losses_count).to_f * 100).round
    self.image.win_pct = winner_pct
    self.image.save

    loser_pct = (self.loser.wins_count.to_f / (self.loser.wins_count + self.loser.losses_count).to_f * 100).round
    self.loser.win_pct = loser_pct
    self.loser.save
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
lentil-0.9.1 app/models/lentil/battle.rb
lentil-0.9.0 app/models/lentil/battle.rb
lentil-0.8.0 app/models/lentil/battle.rb
lentil-0.7.1 app/models/lentil/battle.rb
lentil-0.6.0 app/models/lentil/battle.rb
lentil-0.5.2 app/models/lentil/battle.rb
lentil-0.5.1 app/models/lentil/battle.rb
lentil-0.5.0 app/models/lentil/battle.rb
lentil-0.4.2 app/models/lentil/battle.rb
lentil-0.4.0 app/models/lentil/battle.rb
lentil-0.3.1 app/models/lentil/battle.rb
lentil-0.3.0 app/models/lentil/battle.rb
lentil-0.2.3 app/models/lentil/battle.rb
lentil-0.2.2 app/models/lentil/battle.rb
lentil-0.2.1 app/models/lentil/battle.rb
lentil-0.2.0 app/models/lentil/battle.rb
lentil-0.1.8 app/models/lentil/battle.rb
lentil-0.1.7 app/models/lentil/battle.rb
lentil-0.1.6 app/models/lentil/battle.rb
lentil-0.1.3 app/models/lentil/battle.rb