From 5308ed9825d3e32e7b9e316fda447cb82cc86f4e Mon Sep 17 00:00:00 2001 From: cristian Date: Thu, 20 Nov 2014 21:49:23 +0100 Subject: [PATCH] Don't write the attribute when value is the same as before It will trigger unique & foreign key constraints on the database --- lib/replicate/active_record.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/replicate/active_record.rb b/lib/replicate/active_record.rb index 6acdcee..13d4b89 100644 --- a/lib/replicate/active_record.rb +++ b/lib/replicate/active_record.rb @@ -280,7 +280,11 @@ def create_or_update_replicant(instance, attributes) # write replicated attributes to the instance attributes.each do |key, value| next if key == primary_key and not replicate_id - instance.send :write_attribute, key, value + # Don't write the attribute with the same value as before to avoid + # triggering unique key constraints + unless instance.send(:read_attribute, key) == value + instance.send :write_attribute, key, value + end end # save the instance bypassing all callbacks and validations