DEV Community

Cover image for What the CRUD Active Record
Christian Cedeno
Christian Cedeno

Posted on

What the CRUD Active Record

The following will be a tutorial on CRUD methods used in ruby Active Record to manipulate(read/write)data. Learn more about Active Record here Active Record documentation

CRUD?
What is it, what does it mean? If you aren't familiar already with CRUD, I can help explain the concepts briefly.
CRUD is an acronym for CREATE, READ, UPDATE and DELETE.

Why CRUD?
CRUD is used heavily in programming as a way to communicate with stored data. These so called stored data usually come from API's and or databases. To access or manipulate the information we use CRUD.

CRUD examples
Below I will demonstrate the usage of the crud methods and the output of these invoked methods. I recommend referring to the Active Record documentation for any clarifications! CRUD methods


Create

In active record, we can create objects using either the .new method or the .create method. We use the .create method to create and save a new object. Normally in Active Record the .create method is invoked on a Ruby class to create instances of said class. This method takes in a argument in the form of variables or hash.

//.create method 

adele_album = Album.create(song1: "hello", song2: "someone like you")

or

adele_song = Song.create("hello")

// .new method

album = Album.new
album.song1 = "hello"
album.song2 = "someone like you"
album.save
Enter fullscreen mode Exit fullscreen mode

When creating a new object with the .new method it is required to use the save method for the object to persist in the database unlike the .create method which creates and saves the object all together.


Read

Active Records makes retrieving data from the database fairly easy, think of it as a getter method since it returns based on the method invoked.

for example:

//the .all method will return all instances of album. 

Album.all
=> [song1: "hello", song2: "someone like you"]
Enter fullscreen mode Exit fullscreen mode
//the .find method returns based on the id passed in as a argument. 

Album.find(2)
=> [song2: "someone like you"]
Enter fullscreen mode Exit fullscreen mode

Keep in mind when using Active Record 'id' are autonomously generated by active record when objects are created with the .create method or when saved aft6edr using the .new method.

//the first and last method returns the first object/instance //in the class and the last method returns the last //object/instance

Album.first
=>[song1: "hello"]

Album.last
=>[song2: "someone like you"]
Enter fullscreen mode Exit fullscreen mode

Update

Before we are able to update a object it must first be read. This will give us the exact object we want to update. Update can be done with the .save method or the .update method.
.save method is best used for objects not already present in the database, the .update is best used on a object present in a database that needs to be altered.

//for objects not already in the database we use .save

song = Album.find_by(song1: "hello")
song.song1 = "Rolling in the deep"
song.save

or 

song = Album.find_by(song1: "hello")
song.update(song1: "Rolling in the deep")
Enter fullscreen mode Exit fullscreen mode

check the database to make sure changes were made.


delete

When deleting objects you can read the object or pass in the object as a argument depending on the delete method used. Below will be several examples of the delete method.

//when using the .destroy method, read the object first. 
first_song = Album.first 
first_song.destroy

//the code above can be short handed 
Album.first.destroy 

//we can be specific with the key and values of a class //instance we want to destroy by using the .destroy_by method
//this method takes in an argument of what you want deleted

//however the destroy_by will delete all instances that have //the argument passed in, in this example all song1 keys with //the value "hello" will be deleted.

Album.destroy_by(song1: "hello")

//to delete all instances in a class use the .destroy_all //method

Album.destroy_all

//all instances in the Album class will be deleted.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)