Created
June 11, 2015 21:14
-
-
Save JoshCheek/6a452bc4e39a9aea2088 to your computer and use it in GitHub Desktop.
Snippet from lesson, where we came up with the migrations and models.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'logger' | |
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:' | |
ActiveRecord::Base.logger = Logger.new $stdout | |
ActiveSupport::LogSubscriber.colorize_logging = false | |
ActiveRecord::Schema.define do | |
self.verbose = false | |
create_table :playlists do |t| | |
t.string :name | |
t.integer :currently_playing_id | |
end | |
create_table :playlist_song do |t| | |
t.integer :playlist_id | |
t.integer :song_id | |
end | |
create_table :songs do |t| | |
t.string :title | |
t.string :artist | |
t.string :soundcloud_url | |
end | |
end | |
class PlaylistSong < ActiveRecord::Base | |
self.table_name = :playlist_song | |
belongs_to :song | |
belongs_to :playlist | |
end | |
class Playlist < ActiveRecord::Base | |
has_many :playlist_songs | |
has_many :songs, through: :playlist_songs | |
belongs_to :currently_playing, class_name: 'Song' | |
end | |
class Song < ActiveRecord::Base | |
has_many :playlist_songs | |
has_many :playlists, through: :playlist_songs | |
end | |
our_love = autumn_leaves = nil | |
playlists = [ | |
Playlist.create! do |pl| | |
pl.name = 'Jazz' | |
our_love = pl.songs.build title: "Our Love Is Here To Stay", | |
artist: "Ella Fitzgerald", | |
soundcloud_url: "FILL ME IN WHEN WE GET HERE!" | |
autumn_leaves = pl.songs.build title: "Autumn Leaves", | |
artist: "Chet Baker", | |
soundcloud_url: "FILL ME IN WHEN WE GET HERE!" | |
pl.currently_playing = our_love | |
end, | |
Playlist.create!(name: 'Rock'), | |
Playlist.create!(name: 'Nu Metal'), | |
] | |
playlists | |
# => [#<Playlist:0x007ffaf1a1cd88 | |
# id: 1, | |
# name: "Jazz", | |
# currently_playing_id: 1>, | |
# #<Playlist:0x007ffaf3218900 | |
# id: 2, | |
# name: "Rock", | |
# currently_playing_id: nil>, | |
# #<Playlist:0x007ffaf3222310 | |
# id: 3, | |
# name: "Nu Metal", | |
# currently_playing_id: nil>] | |
Song.all | |
# => [#<Song:0x007ffaf1d3b1e0 | |
# id: 1, | |
# title: "Our Love Is Here To Stay", | |
# artist: "Ella Fitzgerald", | |
# soundcloud_url: "FILL ME IN WHEN WE GET HERE!">, | |
# #<Song:0x007ffaf1d3b078 | |
# id: 2, | |
# title: "Autumn Leaves", | |
# artist: "Chet Baker", | |
# soundcloud_url: "FILL ME IN WHEN WE GET HERE!">] | |
our_love.playlists | |
# => [#<Playlist:0x007ffaf1d6b5c0 | |
# id: 1, | |
# name: "Jazz", | |
# currently_playing_id: 1>] | |
autumn_leaves.playlists | |
# => [#<Playlist:0x007ffaf1d72758 | |
# id: 1, | |
# name: "Jazz", | |
# currently_playing_id: 1>] | |
playlists.map(&:songs) | |
# => [[#<Song:0x007ffaf1d03ee8 | |
# id: 1, | |
# title: "Our Love Is Here To Stay", | |
# artist: "Ella Fitzgerald", | |
# soundcloud_url: "FILL ME IN WHEN WE GET HERE!">, | |
# #<Song:0x007ffaf3198110 | |
# id: 2, | |
# title: "Autumn Leaves", | |
# artist: "Chet Baker", | |
# soundcloud_url: "FILL ME IN WHEN WE GET HERE!">], | |
# [], | |
# []] | |
# >> D, [2015-06-11T14:38:08.576653 #73764] DEBUG -- : (0.3ms) CREATE TABLE "playlists" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "currently_playing_id" integer) | |
# >> D, [2015-06-11T14:38:08.577017 #73764] DEBUG -- : (0.1ms) CREATE TABLE "playlist_song" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "playlist_id" integer, "song_id" integer) | |
# >> D, [2015-06-11T14:38:08.577303 #73764] DEBUG -- : (0.1ms) CREATE TABLE "songs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "artist" varchar, "soundcloud_url" varchar) | |
# >> D, [2015-06-11T14:38:08.646873 #73764] DEBUG -- : (0.1ms) begin transaction | |
# >> D, [2015-06-11T14:38:08.653816 #73764] DEBUG -- : SQL (0.2ms) INSERT INTO "songs" ("title", "artist", "soundcloud_url") VALUES (?, ?, ?) [["title", "Our Love Is Here To Stay"], ["artist", "Ella Fitzgerald"], ["soundcloud_url", "FILL ME IN WHEN WE GET HERE!"]] | |
# >> D, [2015-06-11T14:38:08.654926 #73764] DEBUG -- : SQL (0.1ms) INSERT INTO "playlists" ("name", "currently_playing_id") VALUES (?, ?) [["name", "Jazz"], ["currently_playing_id", 1]] | |
# >> D, [2015-06-11T14:38:08.659181 #73764] DEBUG -- : SQL (0.1ms) INSERT INTO "playlist_song" ("playlist_id", "song_id") VALUES (?, ?) [["playlist_id", 1], ["song_id", 1]] | |
# >> D, [2015-06-11T14:38:08.659886 #73764] DEBUG -- : SQL (0.0ms) INSERT INTO "songs" ("title", "artist", "soundcloud_url") VALUES (?, ?, ?) [["title", "Autumn Leaves"], ["artist", "Chet Baker"], ["soundcloud_url", "FILL ME IN WHEN WE GET HERE!"]] | |
# >> D, [2015-06-11T14:38:08.661082 #73764] DEBUG -- : SQL (0.0ms) INSERT INTO "playlist_song" ("playlist_id", "song_id") VALUES (?, ?) [["playlist_id", 1], ["song_id", 2]] | |
# >> D, [2015-06-11T14:38:08.661449 #73764] DEBUG -- : (0.1ms) commit transaction | |
# >> D, [2015-06-11T14:38:08.661836 #73764] DEBUG -- : (0.1ms) begin transaction | |
# >> D, [2015-06-11T14:38:08.662303 #73764] DEBUG -- : SQL (0.1ms) INSERT INTO "playlists" ("name") VALUES (?) [["name", "Rock"]] | |
# >> D, [2015-06-11T14:38:08.662564 #73764] DEBUG -- : (0.0ms) commit transaction | |
# >> D, [2015-06-11T14:38:08.662768 #73764] DEBUG -- : (0.0ms) begin transaction | |
# >> D, [2015-06-11T14:38:08.663268 #73764] DEBUG -- : SQL (0.1ms) INSERT INTO "playlists" ("name") VALUES (?) [["name", "Nu Metal"]] | |
# >> D, [2015-06-11T14:38:08.663537 #73764] DEBUG -- : (0.0ms) commit transaction | |
# >> D, [2015-06-11T14:38:08.664806 #73764] DEBUG -- : Song Load (0.1ms) SELECT "songs".* FROM "songs" | |
# >> D, [2015-06-11T14:38:08.674902 #73764] DEBUG -- : Playlist Load (0.2ms) SELECT "playlists".* FROM "playlists" INNER JOIN "playlist_song" ON "playlists"."id" = "playlist_song"."playlist_id" WHERE "playlist_song"."song_id" = ? [["song_id", 1]] | |
# >> D, [2015-06-11T14:38:08.676128 #73764] DEBUG -- : Playlist Load (0.0ms) SELECT "playlists".* FROM "playlists" INNER JOIN "playlist_song" ON "playlists"."id" = "playlist_song"."playlist_id" WHERE "playlist_song"."song_id" = ? [["song_id", 2]] | |
# >> D, [2015-06-11T14:38:08.677823 #73764] DEBUG -- : Song Load (0.1ms) SELECT "songs".* FROM "songs" INNER JOIN "playlist_song" ON "songs"."id" = "playlist_song"."song_id" WHERE "playlist_song"."playlist_id" = ? [["playlist_id", 1]] | |
# >> D, [2015-06-11T14:38:08.679345 #73764] DEBUG -- : Song Load (0.0ms) SELECT "songs".* FROM "songs" INNER JOIN "playlist_song" ON "songs"."id" = "playlist_song"."song_id" WHERE "playlist_song"."playlist_id" = ? [["playlist_id", 2]] | |
# >> D, [2015-06-11T14:38:08.679979 #73764] DEBUG -- : Song Load (0.1ms) SELECT "songs".* FROM "songs" INNER JOIN "playlist_song" ON "songs"."id" = "playlist_song"."song_id" WHERE "playlist_song"."playlist_id" = ? [["playlist_id", 3]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment