Rails: select unique values from a column

Rails: How to Select Unique Values from a Column
If you're working with Rails and you have encountered the problem of retrieving only unique values from a specific column, you're not alone. In this blog post, we will address this common issue, provide easy and effective solutions, and finally, we'll discuss why the initial code snippet doesn't work as expected.
The Problem
Let's take a look at the code snippet shared in the question:
ratings = Model.select(:rating).uniq
ratings.each { |r| puts r.rating }The expected behavior here is to select only the unique values from the rating column of the Model table and then print those unique values. However, the code doesn't produce the desired result. Instead, it prints all values, including duplicates.
The Solution
To solve this problem and retrieve only the unique values from a column, we need to slightly modify the code. Here are a couple of solutions:
Solution 1: Using the pluck Method
ratings = Model.pluck(:rating).uniq
ratings.each { |r| puts r }In this solution, we use the pluck method to retrieve an array of values from the rating column. Then, we chain the uniq method to remove any duplicates from the array. Finally, we iterate over the unique values and print them.
Solution 2: Using the distinct Method
ratings = Model.distinct.pluck(:rating)
ratings.each { |r| puts r }In this solution, we utilize the distinct method, which is more concise than using pluck and uniq together. We call distinct on the Model class to ensure that we only retrieve distinct values from the rating column. Then, we use the pluck method to fetch an array of those distinct values. Finally, we iterate over the array and print each value.
Understanding the Issue
Now that we've provided simple solutions to the problem, let's understand why the initial code snippet didn't work as expected.
The documentation link mentioned in the question reveals a crucial detail:
"The API documentation of
uniqalso states that it's used to return an array of distinct objects based on the selected fields."
This means that by using uniq as a method on the relation, it treats the entire rows of the result set as distinct objects, rather than just considering the selected field (:rating in this case).
To summarize, the initial code snippet doesn't work because uniq behaves differently when used within a Rails select statement, leading to all values being printed instead of just the unique ones.
Conclusion
Retrieving unique values from a column in Rails is a common challenge. We provided two straightforward solutions using the pluck and distinct methods to get the desired outcome. Additionally, we explained why the initial code didn't work as expected, offering insights into the behavior of the uniq method within a select statement.
Now that you have the tools to handle this particular Rails scenario, go ahead and implement the solution that suits you best! Remember to share your experience or any further questions in the comments section below.
👉 Have you encountered any other ActiveRecord challenges or interesting use cases? Let us know! We love hearing from our readers and discussing all things Rails.
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



