Skip to content

Commit

Permalink
Bugfix Post .where_hashtag query returning duplicate data (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
NurfitraPujo authored Sep 3, 2021
1 parent ae2a28b commit 086b149
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def to_json(*_args)
def self.parse_raw(raw_posts_data)
posts = []
raw_posts_data.each do |post_data|
puts post_data
post = Post.new(post_data)
posts << post
end
Expand All @@ -150,7 +151,11 @@ def self.all(db_con = DatabaseConnection.instance)
end

def self.where_hashtag(hashtag, db_con = DatabaseConnection.instance)
raw_posts_data = db_con.query("SELECT * FROM post JOIN post_have_hashtags WHERE post_have_hashtags.hashtag = #{hashtag}")
raw_posts_data = db_con.query("SELECT DISTINCT post.id, post.username, post.text, post.timestamp,
post.comment_on
FROM post JOIN post_have_hashtags
WHERE post_have_hashtags.hashtag = #{hashtag} AND
post_have_hashtags.id_post = post.id")
parse_raw(raw_posts_data)
end
end

0 comments on commit 086b149

Please sign in to comment.