title: Rails Conditional Validation in Model from Controller Session Data
body: <p>I am trying to conditionally validate <em>full_name</em> and <em>zip</em> based on whether a visitor is part of a test (Visitors that are part of the test will have certain session data).  I am able to pass true/false from the leads controller to the customer model via customer.visitor_test(), but I can't access <strong>@test</strong> from <strong>in_test?</strong> in the model.  What am I missing?</p>
  
  <p>customer.rb</p>
  
  <pre><code>/* Stripped down code */
  
  class Customer &lt; ActiveRecord::Base
    attr_accessor    :test
  
    validates :full_name, presence: true, if: :not_in_test?
    validates :zip, presence: true, if: :in_test?
  
    def visitor_test(bool)
      @test = bool
    end
  
    def in_test?
      @test
    end
  
    def not_in_test?
      !self.in_test?
    end
  end
  </code></pre>
  
  <p>leads_controller.rb</p>
  
  <pre><code>/* Stripped down code */
  
  class LeadsController &lt; ApplicationController
    def create
      session[:zip] = zip
      session[:email] = email
      session[:full_name] = full_name
      session[:email_opt_in] = email_opt_in
      session[:phone] = phone
  
      listing = Listing.where(id: listing_id).first
  
      customer = create_or_update_customer_from_session(listing)
  
      customer.visitor_test(/* true || false */)
  
      if customer.errors.blank?
        /* Do something */
      else
        /* Something else */
      end
    end
  end
  </code></pre>