I am using a lot of my own validation methods to compare the data from one association to the other. I've noticed that I'm constantly checking that my associations aren't nil before trying to call anything on them, but I am also validating their presence, and so I feel that my nil checks are redundant. Here's an example:
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
I feel like I am repeating myself by checking the existence of enterance and exit within my own validation.
Is there a more "The Rails Way" to do this?