-
-
Notifications
You must be signed in to change notification settings - Fork 910
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
Order of multiple conditions
qualifier in have_many
- v5.3.0
#1561
Comments
Hello! I hope you're doing well. Thank you for raising this issue. After quickly reviewing the source code, I believe you can achieve the desired outcome by combining both where clauses within the conditions method, as shown in the example below. it { should have_many(:active_website_resources)
.conditions(resource: { status: :active }, is_active: true)
.class_name('WebsiteResource') } Please try using this code and see if it works as expected. Let me know if you need any further assistance! |
Hi! I tried many combinations, inline and chained but the result is the same. The culprit is nested condition. Order of conditions matters but attribute names and values of nested condition do not. Only # passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }) # <==
.conditions(is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: true)
.conditions(whatever: { whatever: :whatever })
.class_name('WebsiteResource') } Inline conditions: # passes
it { should have_many(:active_website_resources)
.conditions(is_active: true, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: false, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: false)
.class_name('WebsiteResource') } |
Yes, I dived into the code now, and we are using the shoulda-matchers/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb Lines 60 to 63 in 0394a10
Unfortunately, this method only returns conditions for the queried primary table, in this case, "website_resources", so any nested condition will be discarded. So, making this spec using shoulda is impossible for now 😭 . @mcmire I'd love to hear your thoughts on this. |
Thank you, @matsales28. I appreciate the detailed explanation. |
Not sure if I'm missing something...
Scope in model:
This test passes:
But when conditions are reversed then it fails:
with message:
The text was updated successfully, but these errors were encountered: