title: ActiveRecord ::create returns record with ID = 0
body: <p>I'm having an issue when creating an object with Rails (noticed it when attempting to create an instance with FactoryGirl, where the returned model has <code>id=0</code>, even though it is getting properly persisted and has the right ID in the DB. Other factories are working fine, and this one seems no different. Of note is that the model is backed by a DB view not an actual table, but other models with this same "condition" are working correctly. Any ideas?</p>
  
  <p>My model is:</p>
  
  <pre><code>class Property::Openhouse &lt; ActiveRecord::Base
    self.table_name  = 'property_open_houses_view'
    self.primary_key = 'id'
  
  
    belongs_to :property, :inverse_of =&gt; :openhouses
    attr_protected #allow access to all params
  
    # [SNIP]
  end
  </code></pre>
  
  <p>The factory:</p>
  
  <pre><code>FactoryGirl.define do
    factory :open_house, class: 'Property::Openhouse' do
      property
      mls_system { property.mls_system }
      mls_num { property.mls_num }
      event_date 1.week.from_now
      start_time "10:00AM"
      end_time "6:00PM"
    end
  end
  </code></pre>
  
  <p>What happens in IRB (or tests):</p>
  
  <pre><code>development (main):0 &gt; Property::Openhouse.count
     (0.5ms)  SELECT COUNT(*) FROM `property_open_houses_view`
  =&gt; 5
  development (main):0 &gt; oh = FactoryGirl.create :open_house
  =&gt; #&lt;Property::Openhouse id: 0, property_id: 8, mls_num: "V123460", mls_system: :abcdef, event_date: "2014-04-24 19:57:27", start_time: "10:00AM", end_time: "6:00PM", remarks: nil, created_at: "2014-04-17 21:05:15", updated_at: "2014-04-17 21:05:15", open_house_type: nil, most_recent_import_id: nil, expired_at: nil&gt;
  development (main):0 &gt; oh.persisted?
  =&gt; true
  development (main):0 &gt; oh.id
  =&gt; 0
  development (main):0 &gt; Property::Openhouse.last
    Property::Openhouse Load (0.3ms)  SELECT `property_open_houses_view`.* FROM `property_open_houses_view` ORDER BY `property_open_houses_view`.`id` DESC LIMIT 1
  =&gt; #&lt;Property::Openhouse id: 6, property_id: 8, mls_num: "V123460", mls_system: "abcdef", event_date: "2014-04-24 19:57:27", start_time: "10:00AM", end_time: "6:00PM", remarks: nil, created_at: "2014-04-17 21:05:15", updated_at: "2014-04-17 21:05:15", open_house_type: nil, most_recent_import_id: nil, expired_at: nil&gt;
  development (main):0 &gt; Property::Openhouse.count
     (0.5ms)  SELECT COUNT(*) FROM `property_open_houses_view`
  =&gt; 6 
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong>: I am using MySQL and the view is backedby a single table</p>
  
  <pre><code>CREATE VIEW property_open_houses_view AS
  SELECT
  
  `id`,
  `property_id`,
  `mls_num`,
  `event_date`,
  `start_time`,
  `end_time`,
  `remarks`,
  `created_at`,
  `updated_at`,
  `open_house_type`
  
  FROM property_open_houses;
  </code></pre>
  
  <p><strong>UPDATE Apr 21, 2014</strong></p>
  
  <p>Following the suggestion from @Aaron K, I looked into <a href="https://github.com/rails/rails/issues/5982" rel="nofollow">https://github.com/rails/rails/issues/5982</a>, but I'm afraid that might not be my case either, since my table definition does not have <code>default 0</code> for the ID. </p>
  
  <pre><code># db/schema.rb
  create_table "property_open_houses", :force =&gt; true do |t|
    t.string   "mls_system",            :limit =&gt; 30, :null =&gt; false
    t.integer  "property_id",                         :null =&gt; false
    t.datetime "entered_at"
    t.datetime "modified_at"
    t.string   "mls_num",               :limit =&gt; 30, :null =&gt; false
    t.datetime "event_date"
    t.string   "end_time",              :limit =&gt; 30
    t.string   "start_time",            :limit =&gt; 30
    t.string   "open_house_type",       :limit =&gt; 30
    t.text     "remarks"
    t.string   "remote_sysid"
    t.datetime "created_at",                          :null =&gt; false
    t.datetime "updated_at",                          :null =&gt; false
    t.integer  "most_recent_import_id"
    t.datetime "expired_at"
  end
  </code></pre>
  
  <p>I checked both in <code>schema.rb</code> as well as directly in the database, where the ID in the underlying table is an autoincrementing, non-null, primary key integer. My <code>schema.rb</code> does not include views, we generate them through a rake task that gets run automatically after <code>db:migrate</code> which drops the existing view and recreates it by running the raw SQL shown in my previous update. Interestingly, if I run <code>describe property_open_houses_view;</code> on MySQL, the ID field is listed as Default 0. However, for another table/view pair that I have, even though the same output (ID default 0) shows up in the <code>describe</code>, the Factory Girl models are returned with the appropriate ID.</p>
  
  <p>Incorrect behaviour:</p>
  
  <pre><code>mysql&gt; describe property_open_houses_view;
  +-----------------+-------------+------+-----+---------+-------+
  | Field           | Type        | Null | Key | Default | Extra |
  +-----------------+-------------+------+-----+---------+-------+
  | id              | int(11)     | NO   |     | 0       |       |
   ... SNIP ...
  
  mysql&gt; describe property_open_houses;
  +-----------------+--------------+------+-----+---------+----------------+
  | Field           | Type         | Null | Key | Default | Extra          |
  +-----------------+--------------+------+-----+---------+----------------+
  | id              | int(11)      | NO   | PRI | NULL    | auto_increment |
   ... SNIP ...
  </code></pre>
  
  <p>Correct behaviour:</p>
  
  <pre><code>mysql&gt; describe properties_view;
  +---------------------------+----------------+------+-----+---------+-------+
  | Field                     | Type           | Null | Key | Default | Extra |
  +---------------------------+----------------+------+-----+---------+-------+
  | id                        | int(11)        | NO   |     | 0       |       |
   ... SNIP ...
  
  mysql&gt; describe properties;
  +---------------------------+----------------+------+-----+---------+----------------+
  | Field                     | Type           | Null | Key | Default | Extra          |
  +---------------------------+----------------+------+-----+---------+----------------+
  | id                        | int(11)        | NO   | PRI | NULL    | auto_increment |
   ... SNIP ...
  </code></pre>
  
  <p>One thing I did notice is that when I create an instance of the model that behaves correctly (<code>Property</code>), the resulting SQL <code>INSERT</code> has no <code>id</code> field, but in the misbehaving one (<code>Property::Openhouse</code>), there is such a field, and it is getting provided with a value of 0, which is ignored by the DB which replaces it with the next autoincrementing value.</p>
  
  <pre><code>SQL (1.1ms)  INSERT INTO `property_open_houses_view` (`created_at`, `end_time`, 
  `event_date`, `expired_at`, `id`, `mls_num`, `mls_system`, `most_recent_import_id`, 
  `open_house_type`, `property_id`, `remarks`, `start_time`, `updated_at`) VALUES 
  ('2014-04-21 15:03:25', '6:00PM', '2014-04-14 15:02:27', NULL, 0, 'V123456', 'abcdef',
  NULL, NULL, 1032366, NULL, '10:00AM', '2014-04-21 15:03:25')
  </code></pre>
  
  <p><strong>UPDATE Apr 21, 2014 Part 2</strong></p>
  
  <p>I just realized that FactoryGirl does not seem to be part of the equation, and have thus updated the question title. Creating the model with <code>create</code> raises the same situation</p>
  
  <pre><code>development (main):0 &gt; oh = Property::Openhouse.create(property_id: p.id, mls_num: p.mls_num, mls_system: p.mls_system)
     (0.2ms)  BEGIN
    SQL (0.9ms)  INSERT INTO `property_open_houses_view` (`created_at`, `end_time`, `event_date`, `expired_at`, `id`, `mls_num`, `mls_system`, `most_recent_import_id`, `open_house_type`, `property_id`, `remarks`, `start_time`, `updated_at`) VALUES ('2014-04-21 15:20:37', NULL, NULL, NULL, 0, 'V123457', 'abcdef', NULL, NULL, 1032367, NULL, NULL, '2014-04-21 15:20:37')
     (28.2ms)  COMMIT
  =&gt; #&lt;Property::Openhouse id: 0, property_id: 1032367, mls_num: "V123457", mls_system: :abcdef, event_date: nil, start_time: nil, end_time: nil, remarks: nil, created_at: "2014-04-21 15:20:37", updated_at: "2014-04-21 15:20:37", open_house_type: nil, most_recent_import_id: nil, expired_at: nil&gt;
  </code></pre>