From 086b14980d185aa9d709022b4003f073d2703de8 Mon Sep 17 00:00:00 2001 From: Nurfitra Pujo Santiko Date: Fri, 3 Sep 2021 11:35:06 +0700 Subject: [PATCH] Bugfix Post .where_hashtag query returning duplicate data (#29) --- models/post.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/models/post.rb b/models/post.rb index 372b4a9..9867455 100644 --- a/models/post.rb +++ b/models/post.rb @@ -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 @@ -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