【发布时间】:2010-06-10 21:10:21
【问题描述】:
我使用了很多自己的验证方法来比较一个关联与另一个关联的数据。我注意到,在尝试调用它们之前,我一直在检查我的关联是否为 nil,但我也在验证它们的存在,所以我觉得我的 nil 检查是多余的。这是一个例子:
class House < ActiveRecord::Base
has_one :enterance, :class => Door
has_one :exit, :class => Door
validates_presence_of :enterance, :exit
validate :not_a_fire_hazard
def not_a_fire_hazard
if enterance && exit && enterance.location != exit.location
errors.add_to_base('If there is a fire you will most likely die')
return false
end
end
end
我觉得我在重复自己,在我自己的验证中检查是否存在进入和退出。
有没有更多的“Rails 方式”来做到这一点?
【问题讨论】:
标签: ruby-on-rails validation activerecord