Mastering the Select Method in Rails: A Comprehensive Guide [Part I]
![Mastering the Select Method in Rails: A Comprehensive Guide [Part I]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2FaG8iIT9_iqM%2Fupload%2F64692ee3a97f86f197a653eebefd123a.jpeg&w=3840&q=75)
I'm a passionate software developer.
Search for a command to run...
![Mastering the Select Method in Rails: A Comprehensive Guide [Part I]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fstock%2Funsplash%2FaG8iIT9_iqM%2Fupload%2F64692ee3a97f86f197a653eebefd123a.jpeg&w=3840&q=75)
I'm a passionate software developer.
No comments yet. Be the first to comment.
Components Introduction In order to truly understand props in React, we first need to be clear on what components are. A component is a reusable piece of the user interface that encapsulates structure, styling, and behavior. Instead of repeating the ...

Introduction Context By default, Codespace is using the GITHUB_TOKEN env var (the “Codespaces token”), which can’t create repos. It’s necessary to authenticate gh with a token/account that has the right scopes, or create the repo on the web first. Ob...

Solution Here it is: git commit --allow-empty-message -m "" Explanation To create a Git commit without a message, or with an empty message, you can use the --allow-empty-message flag with the git commit command. git commit --allow-empty-message -m "...

Steps Cleanup phase This phase’s goal is to removes existing containers, volumes, networks and images Stop and remove volumes docker compose down --volumes --remove-orphans The previous command stops and removes containers, networks, and default volu...

Steps Stop the service It’s not mandatory, but before uninstalling, it's a good practice to stop any running Docker services. sudo systemctl stop docker Remove docker packages It’s used to completely and forcefully remove a list of specified packag...

Hey guys, how've you been? It's AC, Alexandre Calaça here. Hope you enjoy this new article.
By the way, I would be glad to receive your feedback about it.
This article Mastering the Select Method in Rails: A Comprehensive Guide - Part I focuses on how to use the Active Record Select method.
It's important to remember that the select method can also be used as an Array class method (Array#select).
So, just to make sure we're on the same page, this article is not going to cover about how to use the Array#select method.
The select(*fields) public method behaves in two distinct ways: one as an Array class method (Array#select) method, the other one as a ActiveRecord::QueryMethods.
In Rails, the select method is used to retrieve a collection of objects from the database that meet certain conditions specified in the method. This is going to become easier as you read this article.
Model.select(:field)
or
Model.select("field")
As an example, considering a Product model, let's retrieve records with only the title field. It would look like this:
Product.select(:title)
or
Product.select("title")
We could also retrieve these records with some conditions. Take a look.
Product.select(:name, :status).where('price < ?', 40)
In this case, the keyword ẁhere was added in order to apply our condition.
If we check the source code, it's noticeable that the select(*fields) uses the splat operator in the argument section. In that case, when we provide arguments, they're going to be converted into an array.
splat operators might be covered later in another article. So far, we just need to understand that we can provide an array or a bunch of arguments separated by comma.
In Rails 3, the select method expects a single argument, which is an array of column names or SQL fragments to select. In case you want to try anyway, the error message would look something like this:
ArgumentError: wrong number of arguments (2 for 0..1)
from /home/alexandre/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/activerecord-3.2.11/lib/active_record/relation/query_methods.rb:70:in `select
In order to provide multiple values in Rails 3, using the select method, an array needs to be used as an argument, due to the splat operator:
Product.select(["type", "status"])
When you pass multiple arguments, Rails 3 interprets them as individual arguments instead of an array, hence the error message.
In Rails 4 and above, the select method can accept multiple arguments:
Product.select("type", "status")
Product.select(:type, :status)
In a successful case scenario, a new Active Record relation is returned containing all elements for which the given block was provided.
selected_products = Product.select(:id, :name).where('price < ?', 10)
[...]
=> #<#<Class:#<ActiveRecord::Relation:0x00563b47398680>>:0x2b1da39cc340>
In the previous code, we would see a list of products that are cheaper than 10, only id and name fields are retrieved.
When the criteria specified in the select method are not met, the method returns an empty ActiveRecord::Relation object.
products = Product.select(:name).where('price > ?', 1.00)
=> #<ActiveRecord::Relation []>
products.empty?
=> true
In this article, we learned how to use the ActiveRecord::QueryMethods select method.
We understood how the select method works differently as an Array class method (Array#select) and as a ActiveRecord::QueryMethods.
We also compared how some characteristics of the method have changed during the release of new Rails versions, especially with regards to parameters and returns.
If you read the article, kudos! you did a great job. You can definitely celebrate!

That's all for today. Thanks for reading the article Mastering the Select Method in Rails: A Comprehensive Guide [Part I].
I hope this article helped you. Let me know if you have any questions.
Your thoughts, suggestions and corrections are more than welcome.
By the way, feel free to drop your suggestions on new blog articles.
Hope to see you next time.