title: Rspec: How to write conditional validation when it also works with validate
body: <p>My Location model which can have a website. The website only has to be present if the location is online. Before it is saved, the website must have the correct format.</p>
  
  <p><strong>location.rb</strong></p>
  
  <pre><code>class Location &lt; ActiveRecord::Base
    attr_accessible :online :website
    validates_presence_of :website, :if =&gt; 'online.present?'
    validates_inclusion_of :online, :in =&gt; [true, false]
    validate :website_has_correct_format, :if =&gt; 'website.present?'
  
    private
  
    def website_has_correct_format
      unless self.website.blank?
        unless self.website.downcase.start_with?('https://', 'http://')
          errors.add(:website, 'The website must be in its full format.)
        end
      end
    end
  end
  </code></pre>
  
  <p>I make a spec to test for this:</p>
  
  <p><strong>location_spec.rb</strong></p>
  
  <pre><code>require 'spec_helper'
  
  describe Location do
  
    before(:each) do
      @location = FactoryGirl.build(:location)
    end
  
    subject { @location }
  
    describe 'when website is not present but store is online' do
      before { @location.website = '' &amp;&amp; @location.online = true }
      it { should_not be_valid }
    end
  end
  </code></pre>
  
  <p>The test fails, bringing to me the error:</p>
  
  <pre><code>Failures:
  
    1) Location when website is not present but store is online
       Failure/Error: it { should_not be_valid }
       NoMethodError:
         undefined method `downcase' for true:TrueClass
       #./app/models/location.rb:82:in `website_has_correct_format'
       #./spec/models/location_spec.rb:72:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>What is the solution to this problem?</p>