-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_follow.rb
More file actions
78 lines (64 loc) · 1.76 KB
/
question_follow.rb
File metadata and controls
78 lines (64 loc) · 1.76 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require_relative 'questions_database'
class QuestionFollow
def self.all
results = QuestionsDatabase.instance.execute('SELECT * FROM question_follows')
results.map { |result| QuestionFollow.new(result) }
end
def self.find_by_question_follow_id(id)
results = QuestionsDatabase.instance.execute(<<-SQL, id)
SELECT
*
FROM
question_follows
WHERE
question_follows.id = ?
SQL
QuestionFollow.new(results.first)
end
def self.followed_questions_for_user_id(user_id)
results = QuestionsDatabase.instance.execute(<<-SQL, user_id)
SELECT
*
FROM
questions
JOIN question_follows ON questions.id = question_follows.question_id
WHERE
question_follows.follower_id = ?
SQL
results.map { |result| Question.new(result) }
end
def self.followers_for_question_id(question_id)
results = QuestionsDatabase.instance.execute(<<-SQL, question_id)
SELECT
*
FROM
users
JOIN question_follows ON question_follows.follower_id = users.id
WHERE
question_follows.question_id = ?
SQL
results.map { |result| User.new(result) }
end
def self.most_followed_questions(n)
results = QuestionsDatabase.instance.execute(<<-SQL, n)
SELECT
*
FROM
questions
JOIN question_follows ON questions.id = question_follows.question_id
GROUP BY
questions.id
ORDER BY
COUNT(question_follows.follower_id)
LIMIT
?
SQL
results.map { |result| Question.new(result)}
end
attr_accessor :id, :follower_id, :question_id
def initialize(options = {})
@id = options['id']
@follower_id = options['follower_id']
@question_id = options['question_id']
end
end