Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panda & Tiger level completed #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions db/migrate/201307152240_create_authors_and_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateAuthorsAndBooks < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :name
t.timestamps
end

create_table :books do |t|
t.string :title
t.references :author
t.timestamps
end
end
end
21 changes: 21 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,24 @@
nbc = Network.create(name: "NBC")
Show.create(name: "Mad Men", day_of_week: "Sunday", hour_of_day: 22, network: amc)
Show.create(name: "Community", day_of_week: "Thursday", hour_of_day: 20, network: nbc)
Show.create(name: "Monday Show", day_of_week: "Monday", hour_of_day: 20, network: amc)
Show.create(name: "Sunday Show", day_of_week: "Sunday", hour_of_day: 20, network: nbc)


Book.delete_all
Author.delete_all

michael = Author.create(name: 'Michael Crichton')
dan = Author.create(name: 'Dan Brown')
daniel = Author.create(name: 'Daniel Suarez')
stuart = Author.create(name: 'Stuart Kauffman')
douglas = Author.create(name: 'Douglas R. Hofstadter')

Book.create(title: 'Sphere', author: michael)
Book.create(title: 'Jurassic Park', author: michael)
Book.create(title: 'The Da Vinci Code', author: dan)
Book.create(title: 'Inferno', author: dan)
Book.create(title: 'Freedom', author: daniel)
Book.create(title: 'Daemon', author: daniel)
Book.create(title: 'At Home In The Universe', author: stuart)
Book.create(title: 'Godel, Escher, Bach: An Eternal Golden Braid', author: douglas)
7 changes: 7 additions & 0 deletions models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Author < ActiveRecord::Base
has_many :books

def to_s
"#{name}, with #{books.count} book(s)"
end
end
7 changes: 7 additions & 0 deletions models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Book < ActiveRecord::Base
belongs_to :author

def to_s
title
end
end
2 changes: 1 addition & 1 deletion models/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Show < ActiveRecord::Base
validates_presence_of :name

def to_s
"#{name} airs at #{hour_of_day}:#{day_of_week}:00 on #{network} "
"#{name} airs at #{day_of_week} #{hour_of_day}:00 on #{network} "
end
end
28 changes: 20 additions & 8 deletions watchman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
require 'bundler/setup'

require "./db/setup"
Dir.glob('./models/*').each { |r| require r}
Dir.glob('./models/*').each { |r| require r }
require "./db/seed"

puts "There are #{Show.count} in the database"

Network.all.each do |network|
puts "Shows airing on #{network}"
network.shows.each do |show|
puts show
end
def horizontal_line
'-' * 80
end
puts "All authors in our database:"

puts horizontal_line
Author.all.each {|author| puts author}
puts horizontal_line

puts "Which author are you interested in?"
name = gets.chomp

author = Author.where(name: name).first

if author
puts "Books written by #{name}:"
author.books.each_with_index {|book, i| puts "#{i+1}. #{book.title}"}
else
puts "Sorry, we don't have any book written by #{name}."
end