Skip to content
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
2 changes: 2 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
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: "Breaking Bad", day_of_week: "Saturday", hour_of_day: 20, network: amc)
Show.create(name: "Californication", day_of_week: "Sunday", hour_of_day: 23, network: nbc)
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 on #{day_of_week} at #{hour_of_day}:00 on #{network} "
end
end
13 changes: 9 additions & 4 deletions watchman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
require "./db/seed"

puts "There are #{Show.count} in the database"
puts
puts "Yo Dawg, i heard you like to watch tv shows?"
puts "So we ask you on which day you want to watch tv shows?"
weekday = gets.chomp.capitalize

Network.all.each do |network|
puts "Shows airing on #{network}"
network.shows.each do |show|
puts show
end
network.shows.where(day_of_week: weekday).each do |show|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will actually execute many queries (1 query for each show, for each network). It's what's called a n+1 query, and a frequent performance problem.

Instead, you could:

Show.where(day_of_week: weekday).each do |show|
  puts show
end

After all, we don't need to group by the network at all here.

puts show
end


end