0

I am working on a Rails 3 app that is needing to run a validation on a virtual field to see if the record already exists... here is my model code:

      #mass-assignment
      attr_accessible :first_name, :last_name, :dob, :gender

      #validations
      validates_presence_of :first_name, :last_name, :gender, :dob
      validates :fullname, :uniqueness => { :scope => [:dob] }

def fullname
    "#{first_name} #{last_name}"
  end

I am not getting any errors, its passing the validation.

dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

1 Answers1

1

I don't think you can do something like that with standard validates. Reason is, it usually is a "simple" sql look-up for uniqueness. But to check uniqueness of what's sent back from your method, it'd have to :

  • Get all entries of your scope
  • For each, execute the fullname method.
  • Add every result in an array while testing for uniqueness

Simple when the data set is small, but then it'd just take ages to do as soon as you reach 10K plus matches in your scope.

I'd personally do this with a standard before_save using the dob scope

before_save :check_full_name_uniqueness

def check_full_name_uniqueness
  query_id = id || 0
  return !Patient.where("first_name = ? and last_name = ? and dob = ? and id != ?", first_name, last_name, dob, query_id).exists?
end

Add error message (before the return):

errors.add(:fullname, 'Your error')

Back in the controller, get the error :

your_object.errors.each do |k, v| #k is the key of the hash, here fullname, and v the error message
  #Do whatever you have to do
end
Anthony Alberto
  • 10,325
  • 3
  • 34
  • 38
  • This is what I got to work: `return !Patient.where(:first_name => first_name, :last_name => last_name, :dob => dob).exists?`... but how do I add a custom error message? – dennismonsewicz Jul 31 '12 at 01:55
  • Awesome thanks! So here is the one thing I am running into... it works on create, but its erroring out on the update... – dennismonsewicz Jul 31 '12 at 02:07
  • when I say erroring out, I mean its throwing the custom error I created. – dennismonsewicz Jul 31 '12 at 02:08
  • Ok edited again ... the second time, your object actually exists and is in the DB. Therefore, of course if we don't exclude it's own ID, the condition will always be true – Anthony Alberto Jul 31 '12 at 02:17