0
我在Rails,User中有一個模型。每次我引用他(例如,find_by_name("NAME")
在script/console
),其字段/屬性last_login
更改爲當前時間。在Ruby on Rails上查找並保存的模型屬性更改
這裏是我的模型:
class User < ActiveRecord::Base
# ACCOUNT INFORATION VALIDATION
validates_presence_of :name
validates_uniqueness_of :name
validates_length_of :name, :in => 4..16
validates_format_of :name, :with => /^\w+$/i, :message => "can only contain letters, numbers and underlines."
validates_presence_of :password
validates_length_of :password, :in => 16..42, :message => "must be between 6 and 32 characters." #10 digits salt included
validates_presence_of :email
validates_uniqueness_of :email
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
# VALIDATES ENERGY AND HP
before_save :check_attributes
def after_find
check_attributes(true)
end
def check_attributes(save=false)
# HP
if self.damage > 0 and self.last_hp_recovery.nil?
self.last_hp_recovery=Time.now
end
while !self.last_hp_recovery.nil? and self.last_hp_recovery<hp_recovery_time.minutes.ago
self.damage-=1
if self.damage == 0
self.last_hp_recovery = nil
else
self.last_hp_recovery+=hp_recovery_time.minutes
end
end
# ENERGY
if self.energy_used > 0 and self.last_energy_recovery.nil?
self.last_energy_recovery=Time.now
end
while !self.last_energy_recovery.nil? and self.last_energy_recovery<energy_recovery_time.minutes.ago
self.energy_used-=1
if self.energy_used == 0
self.last_energy_recovery = nil
else
self.last_energy_recovery+=energy_recovery_time.minutes
end
end
self.save if save
end
def energy_recovery_time
1
end
def hp_recovery_time
1
end
# ATRIBUTES
def hp
@hp=max_hp-self.damage
end
def max_hp
@max_hp=self.base_max_hp+race_hp_bonus
end
def energy
@energy=max_energy-self.energy_used
end
def max_energy
@max_energy=self.base_max_energy+race_energy_bonus
end
def attack
@attack=self.base_attack+race_attack_bonus
end
def defense
@defense=self.base_defense+race_defense_bonus
end
def agility
@agility=self.base_agility+race_agility_bonus
end
# OTHER FUNCTIONS
def generate_salt
self.salt=""
10.times do
self.salt+=rand(10).to_s
end
end
private
# RACE FUNCTIONS
def race_hp_bonus
case self.race
when 0
case self.level
when 1..5
return 100
end
when 1
case self.level
when 1..5
return 110
end
when 2
case self.level
when 1..5
return 90
end
end
return 0
end
def race_energy_bonus
case self.race
when 0
case self.level
when 1..5
return 100
end
when 1
case self.level
when 1..5
return 110
end
when 2
case self.level
when 1..5
return 90
end
end
return 0
end
def race_attack_bonus
case self.race
when 0
case self.level
when 1..5
return 10
end
when 1
case self.level
when 1..5
return 11
end
when 2
case self.level
when 1..5
return 9
end
end
return 0
end
def race_defense_bonus
case self.race
when 0
case self.level
when 1..5
return 10
end
when 1
case self.level
when 1..5
return 11
end
when 2
case self.level
when 1..5
return 9
end
end
return 0
end
def race_agility_bonus
case self.race
when 0
case self.level
when 1..5
return 10
end
when 1
case self.level
when 1..5
return 11
end
when 2
case self.level
when 1..5
return 9
end
end
return 0
end
end
任何想法,爲什麼和如何預防?
非常感謝, 加布裏埃爾。