Skip to content

Commit

Permalink
Wrote AR methods using associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jaf7 committed Feb 8, 2018
1 parent 8c61c28 commit 1f83903
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class Artist < ActiveRecord::Base

def get_genre_of_first_song
#return the genre of the artist's first saved song
self.songs.first.genre
end

def song_count
#return the number of songs associated with the artist
self.songs.count
end

def genre_count
#return the number of genres associated with the artist
self.genres.count
end
end
3 changes: 3 additions & 0 deletions app/models/genre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class Genre < ActiveRecord::Base

def song_count
# return the number of songs in a genre
self.songs.count
end

def artist_count
# return the number of artists associated with the genre
self.artists.count
end

def all_artist_names
# return an array of strings containing every musician's name
self.artists.map{|artist| artist.name}
end
end
5 changes: 4 additions & 1 deletion app/models/song.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ class Song < ActiveRecord::Base
belongs_to :genre

def get_genre_name
self.genre.name
end

def drake_made_this
# when this method is called it should assign the song's artist to Drake
self.artist = Artist.new({"name" => "Drake"})
end
end

end
30 changes: 30 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 5) do

create_table "artists", force: :cascade do |t|
t.string "name"
end

create_table "genres", force: :cascade do |t|
t.string "name"
end

create_table "songs", force: :cascade do |t|
t.string "name"
t.integer "artist_id"
t.integer "genre_id"
end

end

0 comments on commit 1f83903

Please sign in to comment.