# Question about using DISTINCT in ActiveRecord find method

**URL:** <https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096>\
**Category:** Rails\
**Created:** [January 12, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096 "2006-01-12T20:38:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/1 "2006-01-12T20:38:59Z")

</div>

Hi,

Am relatively new to the RoR world. My question is quite simple.

I have a query like such:

shipmentNumbers = Order.find\_by\_sql([“select distinct s.id from orders  
o, line\_items li, shipment\_lines sl, shipments s where o.number=? and  
o.id = li.order\_id and li.id = sl.line\_item\_id and sl.shipment\_id =  
s.id”, number])

The above query works fine. I wanted to do the same query using the  
query below:

shipmentNumbers = Order.find(:all,  
:conditions =\> [“number = ?”, number],  
:joins =\> “as o join line\_items as li on o.id = li.order\_id join  
shipment\_lines as sl on sl.line\_item\_id = li.id join  
shipments as s on sl.shipment\_id = s.id”)

In the above query I need to add the “distinct s.id” equivalent for  
getting the correct results. Does anybody have an idea as to how I would  
use the DISTINCT clause in the find(…) method?

Thanks,

\*\*

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/2 "2006-01-12T20:54:05Z")

</div>

It looks to me like you still thinking about your data in a relational  
way,  
instead of ActiveRecord’s Object mindset. The book Agile Rails has an  
example almost exactly like what you are trying to do. I suggest  
reading  
there.

Rails makes it easy to do things the right way, using ActiveRecord and  
objects. It makes other ways possible, but much much harder.

matt

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/3 "2006-01-12T21:00:09Z")

</div>

I will have a look into that. In the meanwhile, could someone post the  
ActiveRecord way of doing this query?

Thanks,

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/4 "2006-01-12T23:11:05Z")

</div>

Hi Liquid,

Here are the relationships between my table,

order has\_many line\_items. line\_item belongs\_to order  
line\_item has\_many shipment\_lines. shipment\_line belongs\_to line\_item  
shipment has\_many shipment\_lines. shipment\_line belongs\_to shipment

so if i do what you are suggesting, won’t I get duplicate shipments for  
some orders?

I could go though the @shipments and remove the duplicate shipments.  
(Please let me know if I am missing something here).

Thanks,

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/5 "2006-01-12T22:28:16Z")

</div>

Hi Mufaddal,

Assuming that you have the Order object i think you should be able to  
use  
the following

@shipments = @order.line\_items.shipment\_lines.shipments

I think this should work, haven’t tested it tho.

Cheers

---

<div class="post-metadata">

**Author:** ![mkhumri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@mkhumri](https://www.ruby-forum.com/u/mkhumri)\
**Post date:** [January 12, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/question-about-using-distinct-in-activerecord-find-method/95096/6 "2006-01-12T23:26:16Z")

</div>

Hi Mufaddal,

I misunderstood your associations. I see your issue.

you could use the following then

@shipments = @order.line\_items.shipment\_lines.shipments.uniq!

This will collect the array, and then remove duplicate entries.

I hope 😉

Cheers
