Ruby: Using active record to update database in terminal
Published: 2022-06-01 02:29:58


To start off I am assuming you are working in a Ruby project, using Active Record, and have a database up and running.
I am using SQLite for this.

If you are not using Active Record let me tell you that Active Record is a fantastic tool to handle your database. It can be done right in your terminal without any SQL statements. Plus, Active Record is a Ruby gem so all you need is include it in your Gemfile or use 
```
gem install activerecord
```
Check out the Active Record [Documents](https://guides.rubyonrails.org/active_record_basics.html)
To learn how to connect to your database and how to create your tables.

Todays focus is updating your database with Active Record.

To start off I already have 3 users in my database:
 
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpl7w8wn6wykpvj61nfm.png)

We see the user jacque has a password column that is NULL, so lets give them a password.

I like to use a Rakefile to Pry.start my console.

Once the pry is running in the terminal I use User.last to view the user jacque.

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34741u18ctwce3vl2iqz.png)
 
Now lets give the user a new password. I will give them the password "cutiecat1"

In the terminal:

```
user = User.last
user.password = "cutiecat1"
user.save
```

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsg7k4yo1kyu1pg4pcc0.png)
 
Lets double check our work.
Refresh the database and see our update!

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u66uigb6dkn5ouyr2wre.png)
 
Now, lets go ahead and add a new user named Bob.

In the terminal:
```
User.create(username: "Bob", password: "pass1234")
```

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya0nevx59rd404ax4mqd.png)
 
 Looks like it worked! Lets check with typing in the terminal:
```
 User.last
```
 ![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa67z8i5enesa970f2bf.png)
 
Looks good in the terminal, now lets refresh and check our database.

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hutk57xteee1rsf2pqid.png)

Now what if Bob wants to be called "Bobby".
Back in the terminal we write:
```
bob = User.last
bob.update(:username => "Bobby")
```
 ![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5w1x6ek2wkfllsqeaxez.png)
 
Again, lets double check the terminal this time we will just use our variable name:
```
bob
```
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14j8cp2dezlwu65cq4id.png)
 
And refresh the database:

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93nen8x3akuu5vpilh72.png)

Looks good, the database is updated.

Did you notice that I updated Bob's username slightly different that how I updated jacque's password?

Lets update Bob's name one more time to "Rob", but with the first syntax.
In the terminal:
```
bob.username = "Rob"
```
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/357cpsl8vkxzy9c2rsp8.png)
 
Looks like the terminal is showing the name Rob now. But lets check the database

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93nen8x3akuu5vpilh72.png)

Still showing "Bobby" not "Rob". 
But we can fix that with using .save
Back in the terminal:
```
bob.save
```
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owxxfgqulebhlj9v1ewx.png)
 
Now lets check the database:

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar9hl2kdfh2fxmfpg02l.png)

Perfect! Our database is updated.

Lets review those two ways to update.

bob.update(:username => "Bobby")

or

bob.username = "Rob"
bob.save

Now lets delete Bob with .destroy
In the terminal:
```
bob.destroy
```
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhrq9nbs1k976mpkxv7y.png)
 
And Bob is now gone:

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y0ha80vejnc03aivvtt.png)