title: Why is this where().first_or_create generating incorrect SQL in RSpec only?
body: <p>I'm seeing a case where <code>Action.where(:name =&gt; "fred").first_or_create</code> is generating incorrect SQL when run under RSpec, but performs correctly in the console.  I'm baffled.</p>
  
  <p>Here's the model.  The underlying <code>actions</code> table has one field, a string named <code>name</code>:</p>
  
  <pre><code># file: app/models/action.rb
  class Action &lt; ActiveRecord::Base
    def self.internalize(name)
      self.where(:name =&gt; name).first_or_create
    end
  end
  </code></pre>
  
  <p>Here's the rspec test:</p>
  
  <pre><code># file: spec/models/action_spec.rb
  require 'spec_helper'
  describe Action do
    describe 'intern' do
      it 'should create a new name' do  # works
        lambda { Action.internalize("fred") }.should change { Action.count }.by(1)
      end
      it 'should not create duplicate names' do  # fails
        Action.internalize(:name =&gt; "fred")
        lambda { Action.internalize("fred") }.should_not change { Action.count }
      end
    end
  end
  </code></pre>
  
  <p>Here's the failure:</p>
  
  <pre><code>1) Action intern should not create duplicate names
     Failure/Error: Action.internalize(:name =&gt; "fred")
     ActiveRecord::StatementInvalid:
       PG::Error: ERROR:  missing FROM-clause entry for table "name"
       LINE 1: SELECT  "actions".* FROM "actions"  WHERE "name"."name" = 'f...
       : SELECT  "actions".* FROM "actions"  WHERE "name"."name" = 'fred' LIMIT 1
     # ./app/models/action.rb:4:in `internalize'
     # ./spec/models/action_spec.rb:12:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>It appears that when the record exists, <code>Action.where(:name =&gt; "fred").first_or_create</code> is generating the SQL</p>
  
  <pre><code>SELECT "actions".* FROM "actions" WHERE "name"."name" = 'fred' LIMIT 1
  </code></pre>
  
  <p>... which is wrong -- it's looking for a table named "name".</p>
  
  <p>What's odd is that typing the exact same thing into the console performs properly.  And yes,  I remembered (this time) to type <code>rake db:test:prepare</code> before running my RSpec tests.  I'm running</p>
  
  <pre><code>Ruby version              1.9.3 (x86_64-darwin10.8.0)
  Rails version             3.2.1
  RSpec                     2.9.0
  </code></pre>
  
  <p>What the heck is going on here?</p>