diff --git a/app/models/quoted_post.rb b/app/models/quoted_post.rb index 5a4ad076733..2bd06fce81c 100644 --- a/app/models/quoted_post.rb +++ b/app/models/quoted_post.rb @@ -20,22 +20,22 @@ class QuotedPost < ActiveRecord::Base next if uniq[[topic_id,post_number]] uniq[[topic_id,post_number]] = true + begin + # It would be so much nicer if we used post_id in quotes + results = exec_sql "INSERT INTO quoted_posts(post_id, quoted_post_id, created_at, updated_at) + SELECT :post_id, p.id, current_timestamp, current_timestamp + FROM posts p + LEFT JOIN quoted_posts q on q.post_id = :post_id AND q.quoted_post_id = p.id + WHERE post_number = :post_number AND + topic_id = :topic_id AND + q.id IS NULL + RETURNING quoted_post_id + ", post_id: post.id, post_number: post_number, topic_id: topic_id - # It would be so much nicer if we used post_id in quotes - results = exec_sql "INSERT INTO quoted_posts(post_id, quoted_post_id, created_at, updated_at) - SELECT :post_id, p.id, current_timestamp, current_timestamp - FROM posts p - LEFT JOIN quoted_posts q on q.post_id = :post_id AND q.quoted_post_id = p.id - WHERE post_number = :post_number AND - topic_id = :topic_id AND - q.id IS NULL - RETURNING quoted_post_id - ", post_id: post.id, post_number: post_number, topic_id: topic_id - - results = results.to_a - - if results.length > 0 - ids << results[0]["quoted_post_id"].to_i + results = results.to_a + ids << results[0]["quoted_post_id"].to_i if results.length > 0 + rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation + # it's fine end end diff --git a/spec/models/quoted_post_spec.rb b/spec/models/quoted_post_spec.rb index 77bb741339d..4c61952c693 100644 --- a/spec/models/quoted_post_spec.rb +++ b/spec/models/quoted_post_spec.rb @@ -15,9 +15,15 @@ describe QuotedPost do post1 = Fabricate(:post) post2 = Fabricate(:post) - post2.cooked = <
-HTML + post2.cooked = <<-HTML + + HTML QuotedPost.create!(post_id: post2.id, quoted_post_id: 999)When the user will v