title: How to add existing songs to a music library Rails 4
body: <p>What I'm trying to do is add songs that artists have already uploaded to a user library (I have already set up my app so that artists can upload songs). Also, I have set up my code so that an empty user library is created after a user signs up (using the after_create Active Record Callback).</p>
  
  <p>To be more clear, I would like for the user to be able to add songs they see within the site to their library. </p>
  
  <p>However, this is escaping me. I am familiar with CRUD, and have an idea how I would create a library and add existing songs to it, but I am not quite sure how I could add a song to a user library by clicking a button/link saying "Add Song To Library" which would be next to a song, and having it add to the user's existing empty library.</p>
  
  <p>My existing code is below.</p>
  
  <p>User.rb:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    belongs_to :meta, polymorphic: true
  
    before_create :create_empty_profile
  
    after_create :create_empty_library #may not be the best way to do it ¯\_(ツ)_/¯
  
    acts_as_messageable
  
    has_many :playlists
  
    has_many :user_friendships, dependent: :destroy
    has_many :friends, -&gt; { where(user_friendships: { state: 'accepted'}) }, through: :user_friendships
    has_many :pending_user_friendships, -&gt; { where ({ state: 'pending' }) }, class_name: 'UserFriendship', foreign_key: :user_id
    has_many :pending_friends, through: :pending_user_friendships, source: :friend
  
    has_many :chat_rooms, dependent: :destroy
    has_many :chat_messages, dependent: :destroy
  
    has_many :votes, dependent: :destroy
  
    mount_uploader :profile_pic, ProfilePicUploader
  
      def mailboxer_name
        self.name
      end
  
      def mailboxer_email(object)
        self.email
      end
  
    def admin?
      role == 'admin'
    end
  
    def moderator?
      role == 'moderator'
    end
  
    def create_empty_profile
      if is_artist?
        profile = ArtistProfile.new
      else
        profile = UserProfile.new
      end
      profile.save(validate: false)
      self.meta_id = profile.id
      self.meta_type = profile.class.name
    end
  
    def create_empty_library
      library = Library.new
      library.user_id = self.id
      library.save(validate: false)
    end
  
  end
  </code></pre>
  
  <p>Library.rb:</p>
  
  <pre><code>class Library &lt; ActiveRecord::Base
    belongs_to :user
  
    has_many :library_songs
    has_many :songs, through: :library_songs
  
    has_many :library_albums
    has_many :albums, through: :library_albums
  end
  </code></pre>
  
  <p>library_song.rb</p>
  
  <pre><code>class LibrarySong &lt; ActiveRecord::Base
    belongs_to :library
    belongs_to :song
  end
  </code></pre>
  
  <p>library_album.rb</p>
  
  <pre><code>class LibraryAlbum &lt; ActiveRecord::Base
    belongs_to :library
    belongs_to :album
  end
  </code></pre>
  
  <p>libraries_controller.rb</p>
  
  <pre><code>class LibrariesController &lt; ApplicationController
    def index
      @libraries = Library.all
    end
  
    def show
      @library = Library.find(params[:id])
    end
  end
  </code></pre>
  
  <p>I was able to create playlists and add songs to them using the form/controller below.</p>
  
  <p>playlists/new.html.erb:</p>
  
  <pre><code>&lt;h1&gt;New Playlist&lt;/h1&gt;
  
  &lt;%= form_for(@playlist) do |f| %&gt;
    &lt;%= f.text_field :name %&gt;
  
    &lt;% Song.all.each do |song| -%&gt;
      &lt;div&gt;
        &lt;%= check_box_tag :song_ids, song.id, false, :name =&gt; 'playlist[song_ids][]', id: "song-#{song.id}" %&gt;
        &lt;%= song.name %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>playlists_controller.rb:</p>
  
  <pre><code>class PlaylistsController &lt; ApplicationController
    def index
      @playlists = Playlist.all
    end
  
    def show
      @playlist = Playlist.find(params[:id])
    end
  
    def new
      @playlist = Playlist.new
    end
  
    def create
      @playlist = Playlist.create(playlist_params)
      redirect_to @playlist
    end
  
    private
  
    def playlist_params
      params.require(:playlist).permit(:name, song_ids: [])
    end
  end
  </code></pre>
  
  <p>However, the main issue is that in the form above, the playlist is being created along with the existing songs. In this case, I would need to add existing songs to an existing library that is empty.</p>
  
  <p>Any ideas, guys? This would be very helpful. I would be happy to upload any code needed.</p>