title: How are values cast when assigning to a Boolean field in Rails ActiveRecord?
body: <p><strong>Short version of my question</strong></p>
  
  <p>In Rails ActiveRecord, if I have a Boolean field and I assign it something like "<code>abc</code>" or <code>2</code>, then it gets immediately cast to <code>false</code>.  The value <code>1</code> is cast to <code>true</code>, and <code>nil</code> remains as <code>nil</code>.  Why is this the case?  Where can I find Rails documentation (or Ruby documentation) that explains this behavior?</p>
  
  <p><strong>Long version of my question</strong></p>
  
  <p>I am having difficulty understanding how Rails handles attempts to assign values to a <code>Boolean</code> field in a Rails model.  For example, let's say I have a <code>Website</code> model that has a <code>String</code> field called <code>:domain</code> and a <code>Boolean</code> field called <code>:can_ssl</code>.</p>
  
  <p>My migration looks like this:</p>
  
  <pre><code>class CreateWebsites &lt; ActiveRecord::Migration
    def change
      create_table :websites do |t|
        t.string :domain
        t.boolean :can_ssl, :default =&gt; false
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>In my model file, I add some validation rules, so it looks like this:</p>
  
  <pre><code>class Website &lt; ActiveRecord::Base
    validates :domain, :presence =&gt; true
    validates :can_ssl, :inclusion =&gt;  { :in =&gt; [true, false] }
  end
  </code></pre>
  
  <p>Simple enough.  Based on what I've done, I'm <strong>expecting</strong> that <code>:can_ssl</code> can only be set to the values <code>true</code> or <code>false</code>, and nothing else.  Anything else would result in <code>valid?</code> being <code>false</code>.</p>
  
  <p>But once I start playing around in the console, I notice that, as early as the actual assignment statement, the value I provide is being recast to <code>true</code> or <code>false</code> (or <code>nil</code>).  What are the rules governing how a value gets cast as a <code>Boolean</code>?</p>
  
  <p>Examples from the console:</p>
  
  <pre><code>w = Website.new
  w.domain = 'stackoverflow.com'
  w.can_ssl = true
  w.can_ssl    # =&gt; true
  w.valid?     # =&gt; true
  
  w.can_ssl = nil
  w.can_ssl    # =&gt; nil
  w.valid?     # =&gt; false (so far so good)
  
  w.can_ssl = 'abc'
  w.can_ssl    # =&gt; false (How did 'abc' become the value false?)
  w.valid?     # =&gt; true
  
  w.can_ssl = 1
  w.can_ssl    # =&gt; true (I guess it makes sense that 1 casts to true)
  w.valid?     # =&gt; true
  
  w.can_ssl = 2
  w.can_ssl    # =&gt; false (But anything other than 1 casts to false?)
  w.valid?     # =&gt; true
  </code></pre>
  
  <p>So, based on what I've done so far, I <em>think</em> I can conclude the following:</p>
  
  <ul>
  <li>When assigning the value <code>1</code> or <code>true</code> to a <code>Boolean</code> field, the value will immediately get cast as <code>true</code> and then assigned.</li>
  <li>When assigning <code>nil</code> to a <code>Boolean</code> field, the field is actually assigned <code>nil</code>.</li>
  <li>When assigning anything else (such as a <code>String</code> or any number that is not <code>1</code>), then the value will immediately get cast as <code>false</code>.</li>
  </ul>
  
  <p>Am I understanding that correctly?  Am I missing anything?</p>
  
  <p>I'm having difficulty finding documentation the <code>Boolean</code> field type in Rails which can give me clarification on this.</p>