title: RoR: Polymorphic Associations: ID and Type are nil
body: <p>I'm creating an app in Ruby on Rails where users can listen to 2 different types of songs. I want to track each song(and type) that a user listens to. User can listen to a user_song or a master_song.
  My first thought was to use polymorphic associations.</p>
  
  <p>Firstly, my user model looks like this:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base 
  has_many :song_listens, :foreign_key =&gt; "user_id"
  </code></pre>
  
  <p>and then. I declare polymorphic this way:</p>
  
  <pre><code>class SongListen &lt; ActiveRecord::Base
  belongs_to :listenable, :polymorphic =&gt; true
  belongs_to :user
  
  class UserSong &lt; ActiveRecord::Base
  has_many :song_listens, :as =&gt; :listenable, :class_name =&gt; "SongListen"
  
  class MasterSong &lt; ActiveRecord::Base
  has_many :song_listens, :as =&gt; :listenable, :class_name =&gt; "SongListen"
  </code></pre>
  
  <p>My SongListens migration:</p>
  
  <pre><code>class CreateSongListens &lt; ActiveRecord::Migration
    def change
      create_table :song_listens do |t|
        t.integer :user_id
        t.references :listenable, :polymorphic =&gt; true
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>The problem comes in when I try save a user's song listen in the polymorphic table. I get nil for type and id. See -->:</p>
  
  <pre><code>[["created_at", Mon, 07 May 2012 18:25:34 UTC +00:00], ["listenable_id", nil],["listenable_type", nil], ["updated_at", Mon, 07 May 2012 18:25:34 UTC +00:00], ["user_id", 1]]
  </code></pre>
  
  <p>As you can see, the user_id is being set.</p>
  
  <p>My SongListensController:</p>
  
  <pre><code>def create
  @song_listen = current_user.song_listens.build(params[:listenable_id =&gt; :user_song_id])
  @song_listen.save
  end
  
  def create_ms_listen
  @song_listen = current_user.song_listens.build(params[:listenable_id =&gt; :master_song_id])
  @song_listen.save
  end
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>&lt;% @user_songs.each do |user_song| %&gt;
  &lt;td&gt;&lt;%= link_to 'Play Song', user_song.song.url, class: :play, remote: :true %&gt;&lt;/td&gt;
  &lt;% end %&gt;
  
  &lt;% @master_songs.each do |master_song| %&gt;
  &lt;td&gt;&lt;%= link_to 'Play Song', master_song.m_song.url, class: :play, remote: :true %&gt;&lt;/td&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I think this explains the majority of my problem, but for fun, I'm using ajax to create song listen through a music player which sends data through route to SongListen create action:</p>
  
  <pre><code>$('a.play').click(function(e) {
      e.preventDefault();
      $("#jquery_jplayer_1")
          .jPlayer("setMedia", {mp3: this.href })
          .jPlayer("play");
  $.ajax({
    url: '/create_ms_listen.json',
    data: { "master_song_id" : "master_song.id"},
    async: false
    });
  $.ajax({
    url: '/create_us_listen.json',
    data: { "user_song_id" : "user_song.id"},
    async: false
    });
    .....
  </code></pre>
  
  <p>Which goes to my routes:</p>
  
  <pre><code>match "create_us_listen.json" =&gt; "song_listens#create"
  
  match "create_ms_listen.json" =&gt; "song_listens#create_ms_listen"
  </code></pre>
  
  <p>I know my goal is to somehow set the listenable type and id in the SongListen table but so far they have both been nil. What am I doing wrong?</p>