-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Use left joins instead of inner join when searching #1581
Conversation
The tests throw |
Oh, I didn't notice the |
@pablobm should Rails < 5 be dropped from the appraisal as it's EOL? |
I'm sort of averse to dropping Rails <5 support just yet, and it wouldn't be something I'd want to do in this PR. I've opened #1588 for that bit. |
Duplicate of #1386 |
@samnang - A way to retrieve association keys would be using the reflection API. For example: > Order.reflect_on_association(:line_items).foreign_key
=> "order_id" This also works on scopes: > Order.where(address_state: 'NY').reflect_on_association(:line_items).foreign_key
=> "order_id" Having said that, we may end up dropping support for Rails <5 in the next few months (see #1588), so this solution may not live long... or will it? |
Also reported at #1637, which includes a detailed description of the issue. |
Thanks for the response. I think we better to wait instead of introducing complexity by using reflection to construct left join for all tables to join. |
@nickcharlton, instead of just waiting, would you be open to merging a version with essentially |
@livedo, I would be ok with that, with small changes. Instead of detecting the Rails version, I'd prefer def search_results(resources)
inner_or_left_joins(resources, tables_to_join).
where(query_template, *query_values)
end
def inner_or_left_joins(left, right)
# ActiveRecord::QueryMethods#left_join is not supported with Rails 4 and below
if left.respond_to?(:left_join)
left.left_joins(right)
else
left.joins(right)
end
end |
I second @pablobm's suggestion. I'll also say that now the So if it's something we can avoid working around, I'd recommend patching your current install instead of us merging that. |
18d425e
to
8289b1c
Compare
As @nickcharlton says, my suggestion is not relevant any more, since we have dropped support for Rails 4. @samnang - Would you be able to correct the lint warnings (as raised by Hound)? @nickcharlton - Would you be ok to merge this, given the above? |
When searching an association with belongs_to :foobar, optional: true, the current implementation uses INNER JOIN which will filter out resources where the association does not exist. By default when searching associations, it should LEFT OUTER JOIN the association. This feature is supported from Rails 5 onwards.
8289b1c
to
2248af0
Compare
Thanks for your work on this! |
* feat: update `administrate` * fix: use `searchable_fields` instead of `searchable_field` thoughtbot/administrate#1203 * fix: remove `nil` values when listing tables to join thoughtbot/administrate#1581
When searching an association with
belongs_to :foobar, optional: true
, the current implementation usesINNER JOIN
which will filter out resources where the association does not exist.By default when searching associations, it should
LEFT OUTER JOIN
the association.