title: Rails and polymorphic associations
body: <p>I have three models: <code>User</code>, <code>Company</code>, and <code>Subscription</code>. What I am trying to accomplish is a <code>Subscription</code> belongs to either a <code>User</code> OR a <code>Company</code>.</p>
  
  <p>To try accomplish this, I referenced <a href="http://%20https://guides.rubyonrails.org/association_basics.html#polymorphic-associations" rel="nofollow noreferrer">this guide</a>, but I have been unsuccessful as the record creation keeps rolling back.</p>
  
  <p>here's my <code>Company</code> model:</p>
  
  <pre><code># app/models/company.rb
  class Company &lt; ApplicationRecord
      has_many :subscriptions, dependent: :destroy, as: :imageable
  end
  </code></pre>
  
  <p>here's my <code>User</code> model:</p>
  
  <pre><code># app/models/user.rb
  class User &lt; ApplicationRecord
    has_many :subscriptions, dependent: :destroy, as: :imageable
  end
  </code></pre>
  
  <p>and finally, here's my <code>Subscription</code> model:</p>
  
  <pre><code>class Subscription &lt; ApplicationRecord
      belongs_to :imageable, polymorphic: true
  end
  </code></pre>
  
  <p>Now as far as the migration file, this is my <code>Subscription</code> migration file:</p>
  
  <pre><code>class CreateSubscriptions &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :subscriptions do |t|
        t.references :imageable, polymorphic: true, index: true
        t.date :start_date
        t.date :stop_date
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>As far as what I can see, this is pretty much exactly like the guide shows, but it keeps rolling back. Here's the output of the rails console:</p>
  
  <pre><code>Loading development environment (Rails 5.1.6)
  2.5.1 :001 &gt; Subscription.create(imageable_id: 1, start_date: Time.now, stop_date: 2.days.from_now)
     (8.6ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
     (0.2ms)  BEGIN
     (0.3ms)  ROLLBACK
   =&gt; #&lt;Subscription id: nil, imageable_type: nil, imageable_id: 1, start_date: "2018-10-10", stop_date: "2018-10-12", created_at: nil, updated_at: nil&gt; 
  2.5.1 :002 &gt; 
  </code></pre>
  
  <p>Here are the questions that I have:</p>
  
  <ol>
  <li>Why is there an <code>imageable_type</code> field? Is that created by <code>t.references</code> and if so, do I need this? Can I just use <code>imageable_id</code> instead of <code>t.references</code> as the other part of the suggestion shows?</li>
  <li>Why is it rolling back? Are polymorphic associations done differently in Rails 5.x or something by chance?</li>
  <li>According to the graph shown in the guide, it looks like if a picture belongs to <code>imageable_id</code> 4, then if there is an employee AND a production with the ID of 4, then a picture would belongs to both instead of one or the other like I'm trying to accomplish. Correct?</li>
  </ol>