diff --git a/db/migrate/201307152240_create_authors_and_books.rb b/db/migrate/201307152240_create_authors_and_books.rb new file mode 100644 index 0000000..9c88796 --- /dev/null +++ b/db/migrate/201307152240_create_authors_and_books.rb @@ -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 \ No newline at end of file diff --git a/db/seed.rb b/db/seed.rb index 3c028ff..1ddacd1 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -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) diff --git a/models/author.rb b/models/author.rb new file mode 100644 index 0000000..77ff271 --- /dev/null +++ b/models/author.rb @@ -0,0 +1,7 @@ +class Author < ActiveRecord::Base + has_many :books + + def to_s + "#{name}, with #{books.count} book(s)" + end +end \ No newline at end of file diff --git a/models/book.rb b/models/book.rb new file mode 100644 index 0000000..cd2024c --- /dev/null +++ b/models/book.rb @@ -0,0 +1,7 @@ +class Book < ActiveRecord::Base + belongs_to :author + + def to_s + title + end +end \ No newline at end of file diff --git a/models/show.rb b/models/show.rb index 6c82f65..d1d7cb6 100644 --- a/models/show.rb +++ b/models/show.rb @@ -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 diff --git a/watchman.rb b/watchman.rb index ebe9be4..6ee86f6 100644 --- a/watchman.rb +++ b/watchman.rb @@ -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 \ No newline at end of file