Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:36:21+00:00 2026-05-28T19:36:21+00:00

I have two models Customer and Referal. Both have an email attribute. Referal belongs

  • 0

I have two models Customer and Referal. Both have an email attribute. Referal belongs to Customer and Customer has_many Referals.

When I create/update a Referal I want to check that email on the Referal does not equal email on the Customer (parent) object.

Here’s my Referal object: (cut out irrelevant stuff)

class Referal < ActiveRecord::Base
  belongs_to :user
  belongs_to :customer

  validates :name, :presence => true
  validates :email, :presence => true
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  validate :email_cannot_equal_referers

  def email_cannot_equal_referers
    if email == customer.email
      errors.add(:email, "can't be the same as referer's")
    end
  end
end

Here’s Customer:

class Customer < ActiveRecord::Base
  has_many :referals 
  validates_presence_of :name, :email
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true
end

When I try to save Referal I get “undefined method `email’ for nil:NilClass”, which refers to customer.email as it works when I put email==email.
At this point Referal is not written to the database yet, though customer_id seems to be set, as I can use it in the error message (with .to_s), but it doesn’t work either to use this to search for Customer object using Customer.find(customer_id). (Couldn’t find Customer without an ID)

For completeness here is the controller bit that calls the save method on Referal:

@refer=Referal.new
@refer.name=params[:Name2]
@refer.email=params[:Email2]
@refer.message=params[:Message]
@customer.referals << @refer
if @refer.save
  CustomerMailer.refer(@refer).deliver
  CustomerMailer.refer_thanks(@refer).deliver
else
  render 'new_refer'
end

Would greatly appreciate pointers to make this work!
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:36:23+00:00Added an answer on May 28, 2026 at 7:36 pm

    If the customer already exists in the db the validation you have should work.

    If you are trying to create the customer and the referrals at the same time I would use the accepts_nested_attributes_for method through the customer model. I think you might have to add a validation in the customer model that iterates through the referrals.

    class Customer < ActiveRecord::Base
        has_many :referals
        accepts_nested_attributes_for :referals
    end
    

    then in your controller

    @customer.referals << @refer
    if @customer.save
       @customer.referals.each do |r|
          CustomerMailer.refer(r).deliver
          CustomerMailer.refer_thanks(r).deliver
       end
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models Customer and Transaction Customer has_many :transactions Transaction belongs_to :customer Now
I have two models: class Customer(models.Model): (...) class CustomerMemo(models.Model): (...) customer = models.ForeignKey(Customer) text
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models, user & offline_user, and I want to use a dropdown
I have two models (ModelA and ModelB), and FactoryGirl factories for each. I want
I have two models where A has_many B. If I load A including associated
i have two models. Batch and Subject. The relation between them are Batch HAS_MANY
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have two models in my Django application, for the purposes of storing search
I have two models, A and B, and one light, L. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.