ruby-on-railsdistinctsearchlogic

How can I make a searchlogic named scope return DISTINCT (non duplicates?)


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

Company.contact_emails_id_not_null

I only want one Company, no matter how many contact_emails are associated to that Company :through => :contacts


Solution

  • Assuming rails 3:

    Company.contact_emails_id_not_null.select("distinct name_of_your_field")
    

    If rails 2.3.x (please forgive me if it turns out to be false I am unsure)

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

    name_of_your_field can also be * to include all fields.

    Let me know if that helps.