1

I have an EmailContact with validation, like so:

class EmailContact < ActiveRecord::Base
  validates :email, :presence => true, :email => {:message => I18n.t('validations.errors.models.user.invalid_email')},
            :mx => {:message => I18n.t('validations.errors.models.user.invalid_mx')}
end

Here I am validating EmailContact.email.

Then I have a PhoneContact with no validation:

class PhoneContact < ActiveRecord::Base
end

I want to write something like this:

email_contact = EmailContact.create(params)

if email_contact.invalid?
    phone_contact = PhoneContact.create(params)
end

Basically, if the email_contact can't be created due to validation errors, I should then create a phone_contact. How is that possible?

This is what I've tried:

    contact = EmailContact.create(:email => 'a')
       (0.3ms)  BEGIN
       (0.4ms)  ROLLBACK
    ArgumentError: cannot interpret as DNS name: nil

    contact.invalid?
    NoMethodError: undefined method `invalid?' for nil:NilClass

contact just returns nil in this case...

EDIT

It may be that this question needs to go a different direction. Just FYI:

email_contact = EmailContact.new(:email => 'a')
email_contact.valid? 
ArgumentError: cannot interpret as DNS name: nil

email_contact.valid? returns an error instead of returning false as I would expect. I am using the valid_email gem to do my validation on the model.

4
  • It sounds like the mx validation is the one causing this problem because it can't get the host or ip address from the invalid email address that you're passing it. Commented Feb 24, 2014 at 16:43
  • Correct. I think the issue is with how the validation is being done. I'm using a gem valid_email. Even doing email_contact = EmailContact.new(:email => 'a') and running email_contact.valid? throws the same error. It doesn't return true or false, it throws the cannot interpret as DNS name: nil error. Commented Feb 24, 2014 at 16:47
  • 1
    Do you want to do mx validation? It sounds like an overkill to me. If you remove that part, it'll work fine. Commented Feb 24, 2014 at 16:55
  • Valid, mx validation may be overkill - I'll probably remove it and use one of the answers below for now, maybe I can look at bringing it back in later if I need it. Thanks for the help. Commented Feb 24, 2014 at 17:01

2 Answers 2

1

Using invalid? method, you could do something like this :

email_contact = EmailContact.new(params)

if email_contact.invalid?
    phone_contact = PhoneContact.create(params)
else 
    email_contact.save
end

In your case, you used email_contact = EmailContact.create(params).

email_contact would be set to nil if creation fails and you won't be able to call invalid? on a nil object. Check for invalid? before creating the record in database.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use #new and then call #save directly.

email_contact = EmailContact.new(params)

if !email_contact.save
  phone_contact = PhoneContact.create(params)
end

1 Comment

I tried this as well, but !email_contact.save throws an error and doesn't execute the code block within.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.