# Unique column values

**URL:** <https://www.ruby-forum.com/t/unique-column-values/141852>\
**Category:** Rails\
**Created:** [July 1, 2008, 3:36am UTC](https://www.ruby-forum.com/t/unique-column-values/141852 "2008-07-01T03:36:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [July 1, 2008, 3:36am UTC](https://www.ruby-forum.com/t/unique-column-values/141852/1 "2008-07-01T03:36:47Z")

</div>

I cannot find this answer anywhere. Apologies in advance…

I need to get the unique values from a particular column and this code  
seems to work fine:

Street.find(:all, :group =\> :pre\_type)

:pre\_type being the ONLY column I need results for. The above code  
returns the data from all the other columns for each particular row,  
which is more than I need.

Thanks,

TMB

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [July 1, 2008, 4:05am UTC](https://www.ruby-forum.com/t/unique-column-values/141852/2 "2008-07-01T04:05:37Z")

</div>

On Jun 30, 2008, at 9:36 PM, Tm Bo wrote:

> I cannot find this answer anywhere. Apologies in advance…
> 
> I need to get the unique values from a particular column and this code  
> seems to work fine:
> 
> Street.find(:all, :group =\> :pre\_type)

Street.find(:all, :select =\> ‘distinct pre\_type’)

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [July 1, 2008, 4:18am UTC](https://www.ruby-forum.com/t/unique-column-values/141852/3 "2008-07-01T04:18:29Z")

</div>

Hi Tm,

Tm Bo wrote:

> I need to get the unique values from a particular  
> column and this code seems to work fine:
> 
> Street.find(:all, :group =\> :pre\_type)
> 
> :pre\_type being the ONLY column I need results for.  
> the above code returns the data from all the other  
> columns for each particular row, which is more than  
> I need.

The find above should return a row for each unique value of the  
‘pre\_type’  
column. If you only want the values for the column itself, you’ll  
probably  
need to do a select\_by\_sql and specify that that’s the only data you  
want  
returned. Interestingly, it seems the method has disappeared from the  
api  
spec. Hmm… Anyway, just Google it.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [July 1, 2008, 4:37am UTC](https://www.ruby-forum.com/t/unique-column-values/141852/4 "2008-07-01T04:37:12Z")

</div>

On Jun 30, 2008, at 10:17 PM, Bill W. wrote:

> > :pre\_type being the ONLY column I need results for.  
> > the above code returns the data from all the other  
> > columns for each particular row, which is more than  
> > I need.
> 
> The find above should return a row for each unique value of the  
> ‘pre\_type’  
> column. If you only want the values for the column itself, you’ll  
> probably  
> need to do a select\_by\_sql and specify that that’s the only data you  
> want

The :select option of find allows you to specify what columns you want.

Best  
Mike

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [July 1, 2008, 6:49am UTC](https://www.ruby-forum.com/t/unique-column-values/141852/5 "2008-07-01T06:49:01Z")

</div>

SQL it is. I was looking too far 😉

Thanks, guys.
