Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

✨Handle strict_loading for model, record and relationship #20

Merged
merged 11 commits into from
Jul 11, 2023

Conversation

pimpin
Copy link
Member

@pimpin pimpin commented May 9, 2023

To prevent N+1 request issue, enable strict_loading as done in ActiveRecord.

Exemple

class Parent < CouchbaseOrm::Base
    attribute :name
end

class Child < CouchbaseOrm::Base
    attribute :name
    belongs_to :parent, dependent: :destroy
end

parent = Parent.create!(name: 'joe')
child = Child.create!(name: 'bob', parent_id: parent.id)

n_plus_one_protected_child = Child.where(id: child.id).strict_loading.first
n_plus_one_protected_child.parent # raise ActiveRecord::StrictLoadingViolationError

### NB you can also call ActiveRecord::Core#strict_loading!
n_plus_one_protected_child = Child.where(id: child.id).first.strict_loading!
n_plus_one_protected_child.parent # raise ActiveRecord::StrictLoadingViolationError

@pimpin pimpin self-assigned this May 9, 2023
@pimpin pimpin changed the title Add red test on strict_loading for instance (WIP) Add red test on strict_loading for model, record and relationship May 9, 2023
@pimpin pimpin changed the title (WIP) Add red test on strict_loading for model, record and relationship (WIP) Handle strict_loading for model, record and relationship May 9, 2023
@pimpin pimpin changed the title (WIP) Handle strict_loading for model, record and relationship ✨Handle strict_loading for model, record and relationship May 16, 2023
@pimpin pimpin marked this pull request as ready for review May 16, 2023 16:03
@pimpin pimpin marked this pull request as draft May 16, 2023 20:28
@pimpin
Copy link
Member Author

pimpin commented May 16, 2023

In current implementation, calling strict loading on a model will persist...

### this test pass (quite unexpectedly)
Child.strict_loading
expect_strict_loading_error_on_calling_parent(Child.where(id: child.id).strict_loading.first)
expect_strict_loading_error_on_calling_parent(Child.where(id: child.id).first)
expect_strict_loading_error_on_calling_parent(Child.where(id: child.id).last)
expect_strict_loading_error_on_calling_parent(Child.where(id: child.id).to_a.first)
expect_strict_loading_error_on_calling_parent(Child.all.to_a.first)

@pimpin pimpin marked this pull request as ready for review May 30, 2023 15:11
@JMGDoctolib
Copy link
Member

Top 👍

@pimpin pimpin merged commit c1ba1e4 into master Jul 11, 2023
3 checks passed
@pimpin pimpin deleted the new_feature_strict_loading branch July 11, 2023 14:08
pimpin added a commit that referenced this pull request Oct 16, 2023
pimpin added a commit that referenced this pull request Dec 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
3 participants