Ruby On Rails Tutorial => .distinct (or .uniq)

Download Ruby on Rails (PDF)

Ruby on Rails

  • Getting started with Ruby on Rails
  • Awesome Book
  • Awesome Community
  • Awesome Course
  • Awesome Tutorial
  • Awesome YouTube
  • ActionCable
  • ActionController
  • ActionMailer
  • Active Jobs
  • Active Model Serializers
  • ActiveJob
  • ActiveModel
  • ActiveRecord
  • ActiveRecord Associations
  • ActiveRecord Locking
  • ActiveRecord Migrations
  • ActiveRecord Query Interface
    • .delete_all
    • .distinct (or .uniq)
    • .find_by
    • .group and .count
    • .where
    • .where with an array
    • ActiveRecord Bang (!) methods
    • ActiveRecord case insensitive search
    • Get first and last record
    • Includes
    • Joins
    • Limit and Offset
    • Ordering
    • Scopes
    • where.not
  • ActiveRecord Transactions
  • ActiveRecord Transactions
  • ActiveRecord Validations
  • ActiveSupport
  • Add Admin Panel
  • Adding an Amazon RDS to your rails application
  • Asset Pipeline
  • Authenticate Api using Devise
  • Authorization with CanCan
  • Caching
  • Change a default Rails application enviornment
  • Change default timezone
  • Class Organization
  • Configuration
  • Configuration
  • Configure Angular with Rails
  • Debugging
  • Decorator pattern
  • Deploying a Rails app on Heroku
  • Elasticsearch
  • Factory Girl
  • File Uploads
  • Form Helpers
  • Friendly ID
  • Gems
  • I18n - Internationalization
  • Import whole CSV files from specific folder
  • Integrating React.js with Rails Using Hyperloop
  • Model states: AASM
  • Mongoid
  • Multipurpose ActiveRecord columns
  • Naming Conventions
  • Nested form in Ruby on Rails
  • Payment feature in rails
  • Prawn PDF
  • Rails 5
  • Rails 5 API Authetication
  • Rails API
  • Rails Best Practices
  • Rails Cookbook - Advanced rails recipes/learnings and coding techniques
  • Rails Engine - Modular Rails
  • Rails -Engines
  • Rails frameworks over the years
  • Rails generate commands
  • Rails logger
  • Rails on docker
  • React with Rails using react-rails gem
  • Reserved Words
  • Routing
  • RSpec and Ruby on Rails
  • Safe Constantize
  • Securely storing authentication keys
  • Shallow Routing
  • Single Table Inheritance
  • Testing Rails Applications
  • Tools for Ruby on Rails code optimization and cleanup
  • Turbolinks
  • Upgrading Rails
  • User Authentication in Rails
  • Using GoogleMaps with Rails
  • Views

Ruby on Rails

  • Getting started with Ruby on Rails
  • Awesome Book
  • Awesome Community
  • Awesome Course
  • Awesome Tutorial
  • Awesome YouTube
  • ActionCable
  • ActionController
  • ActionMailer
  • Active Jobs
  • Active Model Serializers
  • ActiveJob
  • ActiveModel
  • ActiveRecord
  • ActiveRecord Associations
  • ActiveRecord Locking
  • ActiveRecord Migrations
  • ActiveRecord Query Interface
    • .delete_all
    • .distinct (or .uniq)
    • .find_by
    • .group and .count
    • .where
    • .where with an array
    • ActiveRecord Bang (!) methods
    • ActiveRecord case insensitive search
    • Get first and last record
    • Includes
    • Joins
    • Limit and Offset
    • Ordering
    • Scopes
    • where.not
  • ActiveRecord Transactions
  • ActiveRecord Transactions
  • ActiveRecord Validations
  • ActiveSupport
  • Add Admin Panel
  • Adding an Amazon RDS to your rails application
  • Asset Pipeline
  • Authenticate Api using Devise
  • Authorization with CanCan
  • Caching
  • Change a default Rails application enviornment
  • Change default timezone
  • Class Organization
  • Configuration
  • Configuration
  • Configure Angular with Rails
  • Debugging
  • Decorator pattern
  • Deploying a Rails app on Heroku
  • Elasticsearch
  • Factory Girl
  • File Uploads
  • Form Helpers
  • Friendly ID
  • Gems
  • I18n - Internationalization
  • Import whole CSV files from specific folder
  • Integrating React.js with Rails Using Hyperloop
  • Model states: AASM
  • Mongoid
  • Multipurpose ActiveRecord columns
  • Naming Conventions
  • Nested form in Ruby on Rails
  • Payment feature in rails
  • Prawn PDF
  • Rails 5
  • Rails 5 API Authetication
  • Rails API
  • Rails Best Practices
  • Rails Cookbook - Advanced rails recipes/learnings and coding techniques
  • Rails Engine - Modular Rails
  • Rails -Engines
  • Rails frameworks over the years
  • Rails generate commands
  • Rails logger
  • Rails on docker
  • React with Rails using react-rails gem
  • Reserved Words
  • Routing
  • RSpec and Ruby on Rails
  • Safe Constantize
  • Securely storing authentication keys
  • Shallow Routing
  • Single Table Inheritance
  • Testing Rails Applications
  • Tools for Ruby on Rails code optimization and cleanup
  • Turbolinks
  • Upgrading Rails
  • User Authentication in Rails
  • Using GoogleMaps with Rails
  • Views
Ruby on Rails ActiveRecord Query Interface .distinct (or .uniq)

Fastest Entity Framework Extensions

Bulk Insert Bulk Delete Bulk Update Bulk Merge

Example

If you want to remove duplicates from a result, you can use .distinct():

Customers.select(:country).distinct

This queries the database as follows:

SELECT DISTINCT "customers"."country" FROM "customers"

.uniq() has the same effect. With Rails 5.0 it got deprecated and it will be removed from Rails with version 5.1. The reason is, that the word unique doesn't have the same meaning as distinct and it can be misleading. Furthermore distinct is closer to the SQL syntax.

Got any Ruby on Rails Question?

Ask any Ruby on Rails Questions and Get Instant Answers from ChatGPT AI: ChatGPT answer me! pdf PDF - Download Ruby on Rails for free Previous Next This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow logo rip SUPPORT & PARTNERS
  • Advertise with us
  • Contact us
  • Cookie Policy
  • Privacy Policy
STAY CONNECTED

Get monthly updates about new articles, cheatsheets, and tricks.

Subscribe Cookie This website stores cookies on your computer. We use cookies to enhance your experience on our website and deliver personalized content. For more details on our cookie usage, please review our Cookie Policy and Privacy Policy Accept all Cookies Leave this website

Tag » Activerecord Unique Associations