Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:43:25+00:00 2026-06-18T00:43:25+00:00

In Rails 3, how do i select rows based on unique column values, i

  • 0

In Rails 3, how do i select rows based on unique column values, i need to get all the columns for eg:

SELECT COUNT(DISTINCT date) FROM records

This only returns date column, but i want all the columns (name, date , age , created_at) columns not just the date.

Thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:43:26+00:00Added an answer on June 18, 2026 at 12:43 am

    The issue here is that, by definition, there may be multiple records with the same date. It requires logic in the user space to determine which of the multiple records with the unique date to use. Here’s some code to get those rows:

    Record.select("distinct date").each do |record|
        records = Record.find_by_date record.date
        puts records.count # do something with the records
    end
    

    If what you’re really after is uniqueness among multiple columns, list all the relevant columns in the distinct query:

    Record.select("distinct date, name, age, created_at").each do |record|
        puts record.date
        puts record.name
        puts record.age
        puts record.created_at
        # ``record'' still represents multiple possible records
    end
    

    The fact that you are using distinct means that each “row” returned actually represents n rows, so the DB doesn’t know which of the n rows to pull the remaining columns from. That’s why it only returns the columns used in distinct. It can do no other…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Rails: select unique values from a column Lets say I have 5000
I have an Rails ActiveRecord group query: a = Product.select(date(date) as date, count(id) as
I'm trying to select thousands of rows from a list of IDs in Rails.
I'm using a date select in a rails 3 form. <%=f.date_select :date %> I
How to make this (PostgreSQL) using Rails syntax?: SELECT fld1, fld2 FROM ( SELECT
When I'm saving multiple select from a ruby on rails form it appears to
In my rails form I'm using the select tag. <%= f.select :featured, Timeline::TIMELINE_FEATURED, :prompt
In my rails form i am using a multi select tag code looks like
I have a Rails 3.2 app where a user clicks an image to select
I have a Rails form, where I am trying to insert a Select formfield

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.