module JamRuby
  class Genre < ActiveRecord::Base

    self.primary_key = 'id'

    # bands
    # has_many :genre_players, class_name: "JamRuby::GenrePlayer"
    # has_many :bands, class_name: "JamRuby::Band", through: :genre_players, condition: ['player_type = ?', 'JamRuby::Band'] 
    # has_many :users, class_name: "JamRuby::User", through: :genre_players, condition: ['player_type = ?', 'JamRuby::User'] 


    # music sessions
    has_many :music_sessions, :class_name => "JamRuby::MusicSession"

    # genres
    has_and_belongs_to_many :recordings, :class_name => "JamRuby::Recording", :join_table => "recordings_genres"

    # teachers
    has_many :teachers, :class_name => "JamRuby::Teacher", :through => :teachers_genres
    has_many :teachers_genres, :class_name => "JamRuby::TeacherGenre"

    # jam tracks
    has_many :genres_jam_tracks, :class_name => "JamRuby::GenreJamTrack", :foreign_key => "genre_id"
    has_many :jam_tracks, :through => :genres_jam_tracks, :class_name => "JamRuby::JamTrack", :source => :genre

    def to_s
      description
    end

    def self.jam_track_list
      sql = "SELECT DISTINCT genre_id FROM genres_jam_tracks WHERE genre_id IS NOT NULL"
      Genre.select("DISTINCT(genres.id), genres.*")
        .where("genres.id IN (#{sql})")
        .order('genres.description ASC, genres.id')
    end

  end
end
