- Read Tutorial
- Watch Guide Video
Now that you have a good idea of how the Rails console works, it's time to move on to more advanced work, such asa finding multiple items at a time and putting conditionals in place. Let's see how to go about it now. Start your console window by typing rails c
Finding multiple items in the Rails console
Now, if you want to search records based on a single ID, let's start by having a look at all the records. You don't have to do this, but if you're unsure of the ID and have only a few records, then you can type Project.all
. From this list, let's say we want only Project id:5
, you can use the code:
Project.find(5)
and it will bring up the record with the ID 5
. This method is particularly useful when you want to pull a single record from the database to fix some errors in it. However, you need to know the precise ID of the record to be able to use this method.
Alternately, you can also store it in a variable and just use that variable when needed.
p = Project.find(5)
That was pretty simple, right?
Now, what if I want to find multiple IDs at the same time, and have the results in the form of an array? That's also pretty easy.
Project.find([1,3,5])
This command will bring up the first
, third
and fifth
record for you.
However, they will come back in the form of an array, and you can identify this array by seeing the square brackets at the beginning and end of the results. Compare this with the previous command that returned only one file.
When you have only one record, you can put methods to it, like for example, p.description
Since there is only one value, the method finds that value and displays it for us. The same method will not work for the array.
If you put the array in a variable like before
a = Project.find([1,3,5])
and use it to find the description attribute, you will get an error because it makes no sense. You are not telling Rails which record's description you want, so obviously Rails cannot understand your command and it looks for a method called description
in the array. When it can't find that method, it throws an error.
To access the description attributes of each record, you have to iterate through the array. To do this, your code should be:
a.each do |e| e.description end
These commands will give you the description of each of these three records, as you can see in the image below:
If you don't want to iterate through the array, and want only the last record, you can simply say,
a.last.title.upcase
This command will return the title of the last record in uppercase for you. There is no error here because you are referring to a single record inside the array and not the entire array itself.
This difference between a single item and an array is an important part of programming, and I hope you've understood it well.
Using conditionals
In the previous example, we knew the ID of the project we wanted, so it was easy to retrieve it. Likewise, we knew the position of records in the database, so we could retrieve multiple records directly. Now, what if we don't know the ID and the position, but only the title? In that case, we need to use a command such as:
Project.where(title: "Project2")
This command will search the database for the record that has "Project 2" as its title, and will bring it up for us. You can see the SQL query used by Rails in the image below.
You can search based on any attribute, or even do the opposite, like:
Project.where.not(title: "Project2")
However, it will bring back results in the form of an array since there can be more than one record that matches the above condition.
These conditional queries are useful not only to retrieve records in the console, but you can also put them inside your controller methods to control what gets displayed in your web pages. For example, if you don't want archived pages to be displayed on your blog's home page, you can put in these conditionals inside your controller.
So, in this lesson you've learned how to query based on a single ID, how to query multiple items, how to iterate within an array, difference between an array and a single item and how to put conditionals to get just the data you want.