title: Ruby on Rails:How to validate that id is existed in other columns?
body: <p>I have a column that require whether the id of the User is exist or not 
  My table has these column (id is auto generated)</p>
  
  <p>Table name: users                 </p>
  
  <pre><code>id              :integer         not null, primary key
  first_name      :string
  last_name       :string           
  email           :string           
  sponsor_id      :integer
  created_at      :datetime        not null
  updated_at      :datetime        not null
  password_digest :string           
  </code></pre>
  
  <p>Every value of the <code>User.sponsor_id</code> must be exist in <code>User.id</code> column. Right now, I have this validation for the <code>sponsor_id</code>:</p>
  
  <blockquote>
    <p>validates :sponsor_id,numericality:{ only_integer: true },  presence:
    true</p>
  </blockquote>
  
  <p>I tried this code to make the sponsor_id check whether the id is exist or not </p>
  
  <blockquote>
  <pre><code>#Validate that the user is exist in the data
  def validate_sponsor_id
      errors.add(:sponsor_id,"is not exist")   if User.find_by(id: self.sponsor_id).id ==nil?
  end
  </code></pre>
  </blockquote>
  
  <p>Can anyone help me? thank you </p>