How can I do a search query called scope return DISTINCT (doesn't duplicate?)

I have a searchlogic that searches for not_null in an association that can occur many times, but I only want to display one instance of a UNIQUE / DISTINCT object:

Company.contact_emails_id_not_null

I only need one company, no matter how many contacts contact_emails is associated with this company: through =>: contacts

+3
source share
2 answers

Assuming rails 3:

Company.contact_emails_id_not_null.select("distinct name_of_your_field")

If rails 2.3.x (please forgive me if this turns out to be false, I'm not sure)

Company.contact_emails_id_not_null.find(:all, :select => "distinct name_of_your_field")

name_of_your_field can also be * include all fields.

Let me know if this helps.

+2
source

In Rails 2.3.11, this worked for me ...

@vendor_search = Vendor.searchlogic
@vendors = @vendor_search.paginate({
                                   :page => page,
                                   :per_page => 32,
                                   :order => 'name',
                                   :select => 'DISTINCT vendors.*'
                               })

, - ...

:select => 'DISTINCT vendors.*'
+1

Source: https://habr.com/ru/post/1767990/


All Articles