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

Issue with queries when searching on belongs_to association to the same model #153

Open
aggronerd opened this issue Nov 23, 2016 · 0 comments

Comments

@aggronerd
Copy link

Hi,

I have a model which has a parent to child relationship with itself and would like to search on the model by the presence of absence of a field in the parent. In my attempts to do so I have found that the query generated for these searches creates the joins with a unique alias but does not use it in the subsequent where clause. When doing Entitlement.search_for('parent == "ParentEntitlement'):

 SELECT "entitlements"."id" AS t0_r0, "entitlements"."name" AS t0_r1, "entitlements"."created_at" AS t0_r2, "entitlements"."updated_at" AS t0_r3, "entitlements"."description" AS t0_r4, "entitlements"."parent_entitlement_id" AS t0_r5, "parents_entitlements"."id" AS t1_r0, "parents_entitlements"."name" AS t1_r1, "parents_entitlements"."created_at" AS t1_r2, "parents_entitlements"."updated_at" AS t1_r3, "parents_entitlements"."description" AS t1_r4, "parents_entitlements"."parent_entitlement_id" AS t1_r5 FROM "entitlements" LEFT OUTER JOIN "entitlements" "parents_entitlements" ON "parents_entitlements"."id" = "entitlements"."parent_entitlement_id" WHERE (("entitlements"."name" = 'ParentEntitlement'))

My model has the includes lines:

 has_many :children, foreign_key: :parent_entitlement_id, class_name: to_s, dependent: :restrict_with_error, inverse_of: :parent
belongs_to :parent, foreign_key: :parent_entitlement_id, class_name: to_s, inverse_of: :children

scoped_search in: :parent, on: :name, alias: 'parent', only_explicit: true

I have add an spec in a fork of the code to reproduce the issue:

aggronerd@43677a1

Sadly I am not sure how to fix it.

Please could you advise?

Thanks.

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

No branches or pull requests

1 participant