Quick guide for ActiveRecord methods in Rails

Joaquin Correa
3 min readJul 14, 2021

Student.all()

At first, I was not well acquainted with methods. Fortunately, things ended up panning out for me after getting some help, and this is the reason why I decided to make a quick method guide for Rails beginners.

Warning

This blog post will just be an intro to basic concepts meant for beginners and/or whoever wants to review how methods work. For further reading I highly recommend looking at the more complete resources attached bellow to get familiar with ActiveRecord methods.

Basic Concept

CRUD is the acronym that stands for “Create Read Update and Delete”. Each of these ones is essential The good thing about Rails is that it allows us as programmers to implement these actions when creating our app with a database. For this post we will be using instances of a User object, which will have the attributes of username and a password, to demonstrate our CRUD.

Create: This action is the one that will let declare instances. Generally, this is done through Ruby’s methods .new() and .save(). However to make things easier, Rails comes with the built in method .create that takes as params

#In the editorvee = User.create(username: “vee”, password: “abc123”)eric = User.create(username: “eric”, password: “tacos”)jason = User.create(username: “jtothesun”, password: “cats123”)#In the console
2.7.3 :001 > User.all
User Load (2.3ms) SELECT “users”.* FROM “users” /* loading for inspect */ LIMIT $1 [[“LIMIT”, 11]]=> #<ActiveRecord::Relation [#<User id: 1, username: “vee”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.073987000 +0000”, updated_at: “2021–07–12 14:15:05.073987000 +0000”>, #<User id: 2, username: “eric”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.366708000 +0000”, updated_at: “2021–07–12 14:15:05.366708000 +0000”>, #<User id: 3, username: “jtothesun”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.651829000 +0000”, updated_at: “2021–07–12 14:15:05.651829000 +0000”>]

Read: This action is the one that will let select an instance based on a condition. There are three different methods it can be accomplished. Through the .find(), which takes only as an argument the id of the instance we are looking for. It returns either the instance or the error message; through .find_by(), which can take a hash of attributes belonging to what we are looking for. It returns either the desired instance or an nil value. Finally, through .where(); which also takes a hash of attributes, but instead of returning just one instance, it will either return an array of all of the instances with matching criteria or an empty one.

#Suppose we want to look for a user with the username “jtothesun”, in the console we would type:2.7.3 :002 > User.find_by(username: “jtothesun”)User Load (46.3ms) SELECT “users”.* FROM “users” WHERE “users”.”username” = $1 LIMIT $2 [[“username”, “jtothesun”], [“LIMIT”, 1]]=> #<User id: 3, username: “jtothesun”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.651829000 +0000”, updated_at: “2021–07–12 14:15:05.651829000 +0000”>

Update: Update is the one that will let us change anything we desire from our database. This is done through the .update() method that takes the hash of attributes that will be updated.

#Suppose we want to change user vee’s password, in the editoruser_to_update = User.find_by(username: “vee”)user_to_update.update(password: “newPassword”)

Delete: Delete lets destroy a record in the database. All is needed is to call the .destroy() method next to the selected instance.

#In the editorpotential_user = User.find_by(username: “jtothesun”)potential_user.destroy()#In the console2.7.3 :003 > User.allUser Load (2.3ms) SELECT “users”.* FROM “users” /* loading for inspect */ LIMIT $1 [[“LIMIT”, 11]]=> #<ActiveRecord::Relation [#<User id: 1, username: “vee”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.073987000 +0000”, updated_at: “2021–07–12 14:15:05.073987000 +0000”>, #<User id: 2, username: “eric”, password_digest: [FILTERED], status: nil, profile_pic: nil, created_at: “2021–07–12 14:15:05.366708000 +0000”, updated_at: “2021–07–12 14:15:05.366708000 +0000”>]

Resources for further reading:

Ruby Method Chaining. Linking together multiple Ruby Methods… | by Amanda Nikrant | Medium

Rails models cheatsheet (devhints.io)

Active Record Basics — Ruby on Rails Guides

--

--