Aww… CRUD

Luis Lozano
3 min readMay 12, 2021

--

A lot of what we do with our web applications is gather data. We could gather all the data in the world depending on how we build our application. Awesome right? This gives us what we need to make the customer happy or the information to scale our business. Unfortunately, all that data is useless if we cannot use it the right way. Applications need to be able to perform CRUD, which are the basic ways that we operate on stored data and can persist it. CRUD stands for Create, Read, Update, and Delete. The models in our application should be able to provide these types of functionalities in some way.

Create is the action that allows the user to create a new record in an application’s database. When creating a new instance, we give it new values to create the new record. The following is an example of a create method inside of a Rails application. In it, we are working with a list of users with name, username, and password as its values.

Read action is the what the second letter of CRUD stands for. Even though create is the first action, I have found that in most of my applications I have started with the read action. Its such a small thing that seems to be common sense, but I do remember it throwing me off when I first started learning CRUD. It displays our information onto a page when needed. There are two methods used for this action: index and show. Index will render all instances of a record while show will render a single one. Below is an example of these methods inside the same Rails application. In the index we call all instances of the User class while in show we find each one by their id to render them.

Update is the method that updates the database record itself. The function supplies new values for our instance of user to change the current values. In our user method below, we change the user’s name, username, and password.

Delete, as with the other actions, does exactly what it implies. It allows the user to completely remove a record from the database. First, the function will find the instance by a value then delete it with the destroy method. In the example below I use the instance’s id value to access it then delete it.

--

--