Unique column values

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

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

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

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

SQL it is. I was looking too far :wink:

Thanks, guys.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs