-
Notifications
You must be signed in to change notification settings - Fork 0
/
critic.rb
42 lines (35 loc) · 831 Bytes
/
critic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Critic < Model
def self.table_name
"critics"
end
attr_reader :id
def initialize(options)
@id = options["id"]
@screen_name = options["screen_name"]
end
def reviews
results = RestaurantReview.find_by_critic_id(id)
results.map{|result| RestaurantReview.new(result)}
end
def average_review_score
query = <<-SQL
SELECT AVG(r.score) score
FROM critics c JOIN restaurant_reviews r
ON c.id = r.critic_id
WHERE c.id = ?
SQL
RestaurantsDatabase.instance.execute(query, id)[0]["score"]
end
def unreviewed_restaurants
query = <<-SQL
SELECT *
FROM restaurants
WHERE id NOT IN
(SELECT DISTINCT r.restaurant_id
FROM chefs c
JOIN restaurant_reviews r
ON r.critic_id = c.id
WHERE c.id = 1)
SQL
end
end