I’m fairly new to ruby on rails. I’m using ruby 1.9.3 and rails 3.2.6. My model contains Pilots which have many Checkins. I am trying to add some validation to my model such that a pilot may not add a new checkin which matches the last checkin that pilot made.
class Checkin < ActiveRecord::Base
belongs_to :pilot
…
validate :must_not_match_last_checkin
def ==(comparison_checkin)
self_compare_id = checkin_compare_id self
comparison_compare_id = checkin_compare_id comparison_checkin
if self_compare_id != comparison_compare_id
return true
else
return false;
end
end
private
def must_not_match_last_checkin
if self == self.pilot.checkins.last
errors.add(:base, "Checkin is only valid if it differs from your previous checkin")
end
end
def checkin_compare_id checkin
diff = ""
if !checkin.nil?
diff = checkin.location + checkin.description
end
return diff
end
end
I’ve tested the custom equality method in other situations and it appears to work fine. However when I use the above model the model error always occurs even if the description/location is different.
In your comparison operator:
should really be