Ransack: search associations return duplicated result

Created on 26 Jul 2012  路  11Comments  路  Source: activerecord-hackery/ransack

Hello

ransack is awesome tool, but I have one question.

I have a table A which "has_many" B, and i search A by ":B_A_id_eq", ransack returns more than 2 records even I use result(:distinct => true), because there are more than 2 records in B which associate with same record in table A

Is there anyway to solve this problem?

Thanks

David H

Most helpful comment

Thanks for the quick reply! I will create an application asap

by the way, result(:distinct => true).to_sql shows correct SQL query

All 11 comments

Could you please create an application that reproduces the issue so that I can use it to inspect the problem? Thanks!

Thanks for the quick reply! I will create an application asap

by the way, result(:distinct => true).to_sql shows correct SQL query

+1

Would love to see this get fixed if it hasn't already, using distinct true is great if you're working with a small data set... but it can cause a full table scan.

Do we know what the root cause of this issue is?

I've found that if params[:q] contains key value pairs where the value is empty string, the SQL that is generated will chain LEFT OUTER JOINS, leading to lots of duplication.

You can get rid of this duplication by removing the empty key value pairs manually like so:

params[:q].delete_if{|k, v| v.blank? }

But, ideally the ransack gem would do this for you so this logic isn't spread all throughout your application.

Actually not sure if these are connected -- I was getting duplicated search results for a different reason.

Closed

Why was this Issue closed? I still get duplicated results when filtering over has_many associations.

Still getting duplicate results here as well.

@mortik @stcho any chance you could create a repo with the issue and point it here?

Closing. This would benefit from an example repo or better still a PR with a failing test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanfcarroll picture seanfcarroll  路  3Comments

MatsumotoHiroko picture MatsumotoHiroko  路  4Comments

seanfcarroll picture seanfcarroll  路  4Comments

sebaas picture sebaas  路  3Comments

maduhaime picture maduhaime  路  5Comments