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

Tiger Level #19

Open
wants to merge 1 commit 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
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
Show.delete_all
amc = Network.create(name: "AMC")
nbc = Network.create(name: "NBC")
cbs = Network.create(name: "CBS")
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: "Law and Order:SVU", day_of_week: "Thursday", hour_of_day: 22, network: nbc)
Show.create(name: "How I met Your Mother", day_of_week: "Monday", hour_of_day: 20, network: cbs)
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} "
"#{id.inspect}: #{name} airs at #{day_of_week}:#{hour_of_day}:00 on #{network} "
Copy link
Member

Choose a reason for hiding this comment

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

here you use the "inspect" method on id ---- each object has both a to_s and an inspect method, but for different uses. Inspect should only be used for IRB style introspection. Basically looking at an object. Here you could just do:

"#{id}: #{name} airs at #{day_of_week}:#{hour_of_day}:00 on #{network} "

end
end
32 changes: 32 additions & 0 deletions watchman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,35 @@
puts show
end
end

def search_again
while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_show
else
break
end
end
end


def find_show
puts "What day do you want to watch TV?"
day = gets.chomp

shows = Show.find(:all, :conditions => {:day_of_week => day})
Copy link
Member

Choose a reason for hiding this comment

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

this is Rails 2.x style finders --- where you call "find" and give it a list of conditions. While this will work, it's pretty old school at this point. What you want to use are "scopes".

shows = Show.where(:day_of_week => day)

The cool thing about scopes is you can chain them:

shows = Show.where(:day_of_week => day).where(:network => "AMC")

By default, this will return all results, but you can add a ".first" to it to return the first result.


unless shows.empty?
Copy link
Member

Choose a reason for hiding this comment

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

I don't have many rules on Ruby, but one is "no unless/else". I would rather see this as:

if shows.any?
  # loop throughs shows
else
  puts "nada"
end

shows.each do |show|
puts show
end
else
puts "There was nothing found for #{day}"
end
search_again
end

find_show