From 0acc96c94e2cf99275f6145098717faf9c380641 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 23 Jul 2013 09:48:18 +1000
Subject: [PATCH] work around for
 http://meta.discourse.org/t/activerecord-statementinvalid-exception-raised-in-models-user-action-rb/7275/4

---
 app/models/user_action.rb       | 10 ++++++++--
 spec/models/user_action_spec.rb |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/app/models/user_action.rb b/app/models/user_action.rb
index d702656c0..0660f2429 100644
--- a/app/models/user_action.rb
+++ b/app/models/user_action.rb
@@ -163,10 +163,16 @@ ORDER BY p.created_at desc
   end
 
   def self.log_action!(hash)
-    require_parameters(hash, :action_type, :user_id, :acting_user_id, :target_topic_id, :target_post_id)
+    required_parameters = [:action_type, :user_id, :acting_user_id, :target_topic_id, :target_post_id]
+    require_parameters(hash, *required_parameters)
     transaction(requires_new: true) do
       begin
-        action = new(hash)
+
+        # protect against dupes, for some reason this is failing in some cases
+        action = self.where(hash.select{|k,v| required_parameters.include?(k)}).first
+        return action if action
+
+        action = self.new(hash)
 
         if hash[:created_at]
           action.created_at = hash[:created_at]
diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb
index 9f0dfc13a..c09c50e0f 100644
--- a/spec/models/user_action_spec.rb
+++ b/spec/models/user_action_spec.rb
@@ -87,6 +87,9 @@ describe UserAction do
       stats_for_user(u).should == [UserAction::NEW_TOPIC]
       stream_count(u).should == 1
 
+      # duplicate should not exception out
+      log_test_action
+
     end
   end
 
@@ -107,6 +110,7 @@ describe UserAction do
     it "creates a new stream entry" do
       PostAction.act(liker, post, PostActionType.types[:like])
       likee_stream.count.should == @old_count + 1
+
     end
 
     context "successful like" do