title: RoR - check if record exist before build
body: <p>I have two models</p>
  
  <pre>
  Address.rb
    belongs_to :zip
    accepts_nested_attributes_for :zip
  
  Zip.rb
    has_many :addresses
  </pre>
  
  <p>on the form for a new Address I also create a Zip. But i want to check if the inserted Zip already exists. If it does it should return the existing Zip, if it doesn't it should create a new one</p>
  
  <pre>
  AddressController
    def new
      @address = Address.new
      @address.build_zip
    end
  </pre>
  
  <p>I saw on StackOverFlow a similar <a href="https://stackoverflow.com/questions/4978893/how-to-check-if-a-record-exists-before-creating-a-new-one-in-rails3">question</a> without the answer I was hopping for... Someone suggested:</p>
  
  <pre>
    before_create :check_zip_exists
  
    def check_zip_exists
      @zip = Zip.find_by_cp1_and_cp2(self.cp1, self.cp2)
      if @zip!=nil
        #
      end
    end
  </pre>
  
  <p>what should be at <code>#</code> in order to associate the existing Zip to the Address instead creating a new one?</p>