From 3f05224e1d36156a0f5ed1e75d769d99e25c5cdb Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Wed, 1 Jun 2022 09:32:40 -0700 Subject: [PATCH 1/2] Fix typo --- spec/counter_culture_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/counter_culture_spec.rb b/spec/counter_culture_spec.rb index c674278..d727801 100644 --- a/spec/counter_culture_spec.rb +++ b/spec/counter_culture_spec.rb @@ -165,7 +165,7 @@ def yaml_load(yaml) expect(user2.reload.review_approvals_count).to eq(69) end - it "works with multiple saves in one transcation" do + it "works with multiple saves in one transaction" do user = User.create product = Product.create From ffd172062876180ecbf7881cb7d6816f9ec23bbd Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Wed, 1 Jun 2022 09:32:45 -0700 Subject: [PATCH 2/2] Simplify FK lookup --- lib/counter_culture/counter.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/counter_culture/counter.rb b/lib/counter_culture/counter.rb index 19b753d..ebb2fd5 100644 --- a/lib/counter_culture/counter.rb +++ b/lib/counter_culture/counter.rb @@ -165,15 +165,13 @@ def full_primary_key(klass) def foreign_key_value(obj, relation, was = false) original_relation = relation relation = relation.is_a?(Enumerable) ? relation.dup : [relation] - + if was first = relation.shift foreign_key_value = attribute_was(obj, relation_foreign_key(first)) klass = relation_klass(first, source: obj, was: was) if foreign_key_value - value = klass.where( - "#{klass.table_name}.#{relation_primary_key(first, source: obj, was: was)} = ?", - foreign_key_value).first + value = klass.where(relation_primary_key(first, source: obj, was: was) => foreign_key_value).first end else value = obj