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

Support nested children in Arel::Nodes::And #1279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ciihla
Copy link

@ciihla ciihla commented Mar 8, 2022

Fixes #925.
Fixes #1240.

@ciihla ciihla changed the title support nested children in Arel::Nodes::And #925 support nested children in Arel::Nodes::And Mar 8, 2022
@deivid-rodriguez deivid-rodriguez changed the title #925 support nested children in Arel::Nodes::And Support nested children in Arel::Nodes::And Mar 8, 2022
@scarroll32
Copy link
Member

This looks great but do we have test coverage?

@deivid-rodriguez
Copy link
Contributor

Nope, also I tried to reproduce the issue and couldn't.

@scarroll32
Copy link
Member

@ciihla thank you for this contribution, is it possible to add a test (that would fail first without your PR) ?

@ciihla
Copy link
Author

ciihla commented Mar 14, 2022

@ciihla thank you for this contribution, is it possible to add a test (that would fail first without your PR) ?

The test is very specific, maybe it has to do something with combination of multitenancy(acts_as_tenant) + postgres_ext-serializers + our specific usage (hierarchy sql condition). but basically our test fails within this:

let!(:column) { create(:column, space: space) }
    let!(:column2) { create(:column, space: space) }
    let!(:column_value) { create(:column_value, space: space, feature: feature_1, column: column) }
    let!(:column_value2) { create(:column_value, space: space, feature: feature_2, column: column2) }

    let(:params) { { q: { column_values_column_id_not_eq: column.id, id_in: [feature_1.id, feature_2.id] } } }

    it 'returns all data' do
      expect(space.features
                      .select('features.*, case when count(childs.id) > 0 then true else false end as has_children')
                      .joins('left join features childs on childs.parent_id = features.id')
                      .group('features.id').ransack(params[:q]).result.to_a.count).to eq(1)
    end

error:

  NoMethodError:
       undefined method `children' for nil:NilClass
     # /Users/jan.uhlar/.rvm/gems/ruby-3.0.3@pb-backend/gems/ransack-2.5.0/lib/ransack/adapters/active_record/context.rb:146:in `block in remove_association'
     # /Users/jan.uhlar/.rvm/gems/ruby-3.0.3@pb-backend/gems/ransack-2.5.0/lib/ransack/adapters/active_record/context.rb:145:in `delete_if'
     # /Users/jan.uhlar/.rvm/gems/ruby-3.0.3@pb-backend/gems/ransack-2.5.0/lib/ransack/adapters/active_record/context.rb:145:in `remove_association'
     # /Users/jan.uhlar/.rvm/gems/ruby-3.0.3@pb-backend/gems/ransack-2.5.0/lib/ransack/adapters/active_record/ransack/nodes/condition.rb:10:in `block in arel_predicate'
   

Sorry I dont have much time to elaborate more :(

@deivid-rodriguez
Copy link
Contributor

Thanks @ciihla, it definitely makes sense to me that a third gem is involved here. If you find time to isolate this to a sample app, it'd be great. Even temporarily removing either of these gems from your application to see if the issue still reproduces and further isolate the culprit would be great.

@shannondoah
Copy link

shannondoah commented Jan 19, 2024

We encountered another use case that this patch seems to fix: querying a scoped association where the association model also has a default scope (multiple conditions). Here is a standalone test that exposes the error, and a copy with the patch applied that executes without error.

Here is another example reproducing the failure with a different set of conditions.

@tappleby
Copy link

@shannondoah I just ran into this same issue, have you been running this patch in production?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

All negative search in deep join not working Ransack not_eq not working
5 participants