Whether you have made simple queries with SQL language to get records in some relational database or you have experience as a complex database administrator, when starting it might seem extremely difficult to learn the syntax to select only what you require, or which table to join with which to make the necessary consultation. In this article, we are going to know the magic that Ruby performs together with her gem "ActiveRecord" to automate and facilitate the management of records in a relational database. When ActiveRecord maps classes that inherit from the ActiveRecord :: Base class, it builds common query methods on simple class methods. The same SQL queries are made by ActiveRecord, but the abstract ones to short story methods like , , , among others, in which we as developers do not see what is happening. find () group () select () You will realize that you can perform the same operations in a faster way and with a much more natural syntax. I felt it was much easier as it required writing less code plus those methods were named more intuitively. For example, if we want to get the user with id: 43, in SQL it would be something like this: * (user.id = ) SELECT FROM user WHERE 43 LIMIT 1 With the ActiveRecord method syntax it would be: find() User.find( ) Returns => 43 # <User id: 43, title: "The Bes Taquero", name: "Juan Pablo Gil"> The result is the same, you get the User record with the requested id, with the advantage that the syntax is much shorter and easier to understand. We have just used an ActiveRecord method to perform a simple query that finds a record in the “User” table and with it, we could see a bit of the magic in action, however, the query that was executed to obtain was not observed in the terminal that record. In addition to obtaining only one record, you can send the array an ids array: SQL: . * . (?,?) [[ , ], [ , ]] SELECT "user" FROM "user" WHERE "user" "Id" IN "Id" 43 "id" 72 Ruby: User.find ([ , ]) Returns Array => 43 72 # [< User id: 43, title: "The Bes Taquero", name: "Juan Pablo Gil">, <User id: 72, title: "CEO", name: "John Doe">] As we can see, executing the find method shows the SQL query that was executed, in addition to the result, obtained this time an Array with the 2 records of the "User" table that we required. Essential methods to search for records in your database : Returns a record without any implicit order. take * SELECT FROM user LIMIT 1 Active record: User.take Returns => # <User: id: 0, title: "Developer 1 " name: "John Doe 1"> : Returns the first record ordered by its primary key. first * user.id SELECT FROM user ORDER BY ASC LIMIT 1 Active record: User.first Returns => # <User: id: 0, title:"Developer 1 " name: "John Doe 1"> Returns the last record ordered by its primary key (default). last: * user.id SELECT FROM user ORDER BY DESC LIMIT 1 Active Record: .last => # < : id: , title: "Developer 101", : "John Doe 101"> User Returns User 100 name In a collection that is using the order method, last will return the last record ordered by the specified attribute. ordered .* SELECT "users" FROM "users" ORDER BY name Active Record: User.order( ).first Returns => 'name' # <User: id: 0, title: "Developer 1", author: "John Doe"> : Returns the first record found that meets a certain condition. find_by * (user.name = ) SELECT FROM user WHERE 'Juan Pablo Gil' LIMIT 1 Active Record: User.find_by ( ) Returns => name: 'Juan Pablo Gil' # <User: id: 43, title: "The best taquero", author: "Juan Pablo Gil" Conditional searches The method allows you to specify conditions to limit the returned records. This method represents the part of a SQL query. where WHERE .* (quantity > price > ) SELECT "details" FROM "details" WHERE 47 AND 10 Active Record: Prices.where( , , ) Returns => # [#<Prices: id: , : , : , : >, #<Prices: id: , : , : , : >, #<Prices: id: , : , : , : >] "quantity > ? AND price > ?" 99 1000 61 quantity 100 user_id 15 price 1062 26 quantity 100 user_id 39 price 1863 95 quantity 100 user_id 6 price 1029 Ordered searches As we saw earlier, we can use the order method, it allows you to order the records, based on an indicated parameter. .* . SELECT "projects" FROM "projects" ORDER BY "projects" "date" ASC Active Record: Project.order( ) => [#<project: id: 94, date: 23 May 2020, user_id: 1>, #<project: id: 12, date: 10 Feb 2020, user_id: 2>, #<project: id: 4, date: 28 Sep 2020, user_id: 53> ] :date # Selecting an attribute When performing a search you can specify the specific attributes you need with the method. select . SELECT "projects" "date" FROM "projects" Active Record: Project.select( ) Returns => [ #<project: id: , date: Nov >, #<project: id: , date: May >, #<project: id: , date: Oct >] "date" 0 05 2019 1 19 2020 2 26 2020 Scopes Scopes allow you to specify commonly-used queries, which can be referenced when invoked. They can use all the methods mentioned above. ( ). Usually declared in modes and can receive parameters ex "Age" scope , -> { where( .. ) } < ActiveRecord::Base class User :productive age: 20 50 end When invoking that scope defined in the Book model, the following happens: .* . ? ? [[ , ], [ , ]] SELECT "users" FROM "users" WHERE "users" "age" BETWEEN AND "age" 20 "age" 50 Active Record : User.productive Returns => [ < , , , >, . . . < , , , >] User: id: 1 title: "Developer 1" name: "John Doe " age: 21 User: id: 48 title: "Developer 48" name: "John Doe " age: 48 After learning about some of the most widely used methods to query the database with ActiveRecord, I can recommend that you do many practices to reduce the development time of an application, in addition to consulting the , where you will find the utility, proper syntax, and arguments for each query method. Active Record Query Interface documentation