ruby on rails - can't get model to properly validate -
i have following model. works when don't fill in rep or weight keep getting error: undefined method `*' nil:nilclass
and in calculate_tonnage method.
is there way can check presence of reps or weight, calculate_tonnage en then check if tonnage unique.
i assume solution cannot find how this. have clue best way solve problem?
class submission < activerecord::base belongs_to :user belongs_to :contest validates_presence_of :user_id, :reps, :weight validates_uniqueness_of :tonnage, scope: [:user_id, :contest_id] after_validation :calculate_tonnage # model helpers def calculate_tonnage self.tonnage = self.weight * self.reps end end
sure, change code to:
def calculate_tonnage self.tonnage = weight * reps if weight && reps end
Comments
Post a Comment