Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify fk lookup #353

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/counter_culture/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/counter_culture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down