-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.rb
More file actions
62 lines (48 loc) · 1.23 KB
/
question.rb
File metadata and controls
62 lines (48 loc) · 1.23 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
class Question
attr_accessor :title, :body, :author_id, :id
def self.all
results = QuestionsDatabase.instance.execute( 'SELECT * FROM questions')
results.map {|result| Question.new(result) }
end
def self.most_followed(n)
QuestionFollower.most_followed_questions(n)
end
def initialize(options = {})
@id = options['id']
@title = options['title']
@body = options['body']
@author_id = options['author_id']
end
def create
raise 'already saved!' unless self.id.nil?
QuestionsDatabase.instance.execute(<<-SQL, title, body, author_id)
INSERT INTO
questions (title, body, author_id)
VALUES
(?, ?, ?)
SQL
end
def find_by_id(question_id)
user_hash = QuestionsDatabase.instance.execute(<<-SQL, question_id)
SELECT
*
FROM
questions
WHERE id = (?)
SQL
Question.new(user_hash.first)
end
def find_by_author_id(author_id)
questions_hash = QuestionsDatabase.instance.execute(<<-SQL, author_id)
SELECT
*
FROM
questions
WHERE author_id = (?)
SQL
questions_hash.map {|question| Question.new(question)}
end
def followers
QuestionFollower.followers_for_question_id(@id)
end
end