class PlaylistController < ApplicationController def data @playlist = Playlist.find_by_id(params[:id]) render json: @playlist, include: { playlist_items: { include: { track: { include: { artists: {} }}}}} end def lastfm_top50 artist = Artist.find_by_id(params[:id]) return if artist.nil? playlist = Playlist.new(name: "#{artist.name}: Last.fm TOP 50", artist: artist) LastFM::Artist.get_top_tracks(artist: artist.name)["toptracks"]["track"].each do |track| tracks = Track.joins(:album, :artists).where(name: track["name"], "track_artists.artist_id" => artist.id) playlist.playlist_items << PlaylistItem.new(track_id: tracks.first.id) unless tracks.empty? end cache_for 1.week render json: playlist, include: { playlist_items: { include: { track: { include: { artists: {} }}}}} end end