【发布时间】:2015-10-03 08:50:57
【问题描述】:
我有以下型号。这可行,但是当我不填写代表或重量时,我不断收到错误消息:undefined method `*' for nil:NilClass
这在 calculate_tonnage 方法中。
有没有办法让我检查是否存在代表或重量,然后计算吨位 en 然后检查吨位是否唯一。
我认为这是解决方案,但我找不到如何做到这一点。有没有人知道解决这个问题的最佳方法是什么?
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
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord models