# Getting unique entries from models?

**URL:** <https://www.ruby-forum.com/t/getting-unique-entries-from-models/161547>\
**Category:** Rails\
**Created:** [February 27, 2009, 9:21am UTC](https://www.ruby-forum.com/t/getting-unique-entries-from-models/161547 "2009-02-27T09:21:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [February 27, 2009, 9:21am UTC](https://www.ruby-forum.com/t/getting-unique-entries-from-models/161547/1 "2009-02-27T09:21:58Z")

</div>

I’m using acts\_as\_taggable\_on and I want to get a list of unique  
taggings. How can I do this? In a more general sense, say that I have  
a Product model with attributes name and price. I have these:

id: 1  
Name: Juice  
Price: 5

id: 2  
Name: Juice  
Price: 5

So when I do a Product.find(:all) I’ll get both, but I only want one.  
How can I do this so that no matter now many “duplicates” there are,  
I’ll only get one PLUS all the other UNIQUE products?

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [February 27, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/getting-unique-entries-from-models/161547/2 "2009-02-27T17:25:59Z")

</div>

Mike Chai wrote:

> I’m using acts\_as\_taggable\_on and I want to get a list of unique  
> taggings. How can I do this? In a more general sense, say that I have  
> a Product model with attributes name and price. I have these:
> 
> id: 1  
> Name: Juice  
> Price: 5
> 
> id: 2  
> Name: Juice  
> Price: 5
> 
> So when I do a Product.find(:all) I’ll get both, but I only want one.  
> How can I do this so that no matter now many “duplicates” there are,  
> I’ll only get one PLUS all the other UNIQUE products?

I’m confused. How does this relate to acts\_as\_taggable\_on?

What I see here is that you have a products table that allows the  
product name to be duplicated. This sounds like a bad idea to me. It  
seems more logical to either validate that the product name is unique  
with a unique index in the database to ensure the name is always unique.  
Or identify your products by something other than the name (like maybe  
SKU Number or Item Number). Something exposed to the user that they can  
use to find the right product.

Product.find(:all) is supposed to give you both. That’s what it does. If  
you must insist on doing a distinct select then you’ll have to drop down  
to a lower level like…

Product.find\_by\_sql(“SELECT DISTINCT name, price from products…”)

But, again this seems like a really bad idea because you have no idea  
which one you’re going to get and if you included the id in the distinct  
select then you would still get both anyway.

With all that said, I think you need to reevaluate your application’s  
design.

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [February 27, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/getting-unique-entries-from-models/161547/3 "2009-02-27T18:12:21Z")

</div>

Thanks for the help. That was just an example, not a good one, but I  
just needed to explain the problem I’m having. It had nothing to do  
with acts\_as\_taggable\_on, but I might as well just directly tell my  
problem.

I want to display all the tags that a tagger has used. However, using  
@user.owned\_taggings returns ALL the taggings, including duplicates.  
What I mean by duplicates, is that the taggable\_type and the tag\_id  
are the same. That means if someone used one tag on many different  
Products, it would be a list of that one word.

On Feb 27, 8:25 am, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
