Ruby Database Setup & Common Commands & CRUD
Tags: ruby, sql, crud, notes
Published: 2024-02-08 18:12:17

Using PSQL in the rails database. Explaining set up and then going into common methods for accessing data within the database.

Note ➔ When editing and creating files within your app and outside of your terminal sql editor, you will need to start and restart the rails console to make sure the changes are incorporated. 

## **➔ Commands to start your db for rails**

1. First, check out my other article on using SQL to create a table and the first object inside. {% embed https://dev.to/marywebby/notes-on-databases-and-sql-commands-for-reference-3k01 %} 

2. Next, you will need to change your `config/database.yml` file to add your database you created. For example, `database: my_contact_book`.

3. Next you will be adding your class file to the models folder, so for example, `app/models/class.rb`. In this folder will be, the base set up whats below. `< ActiveRecord::Base` allows `Class` to inherit all properties and methods in ActiveRecord. `class Class < ActiveRecord::Base`\nextLine`end`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hm0mm9wvmoipspq20n2v.png)



4. You are also able to `self.table_name = "contacts"` and more properties to your class to manually enter here, instead of the command line. 

5. You will now need to open the terminal and type in `irb`, which will prompt the "interactive ruby", which is a tool to interactively execute Ruby expressions read from the standard input. 

6. Then you will `require "./app/models/class"` to ensure you have the file you need. 

7. Exit `irb` by executing `exit`, and type `rails c` or `rails console`, which will open the respective ruby PSQL. 



## **➔ Methods to act on your class within the `rails console`.** 
ActiveRecord Relations are very similar to Arrays. Any method that you can call on an Array, you can also call on a Relation; .at, .each, .sample, etc.

- `Class.count` returns an integer 

- `Contact.all` returns all the records in a table 

- `x.at(0)` x being and example for what to replace, and at(0) being where you are looking in the indexes. 

- `c = Contact.all.sample` taking out a random contact and storing it in the variable c. 

- `c.id`, `c.created_at`, `c.first_name`, `c.phone`, now we can call on that specific variable we created and access its contents. 

- `Contact.all.order({ :last_name => :asc, :first_name => :asc, :date_of_birth => :desc })` retrieve info and sorting it in a specific way. 

- `Contact.order(:first_name).reverse_order` same thing with this one. 

- `Contact.where({ :last_name => ["Betina", "Woods"] })` giving specific to try and find a name. 

- `Contact.where({ :first_name => "Mickey" }).or(Contact.where({ :last_name => "Betina" }))` Similar ask to before but instead you are broadening the search. 

## **➔ CREATE READ UPDATE & DELETE** 

#### UPDATE
To update a row, first you need to locate it:
```
c = Contact.where({ :id => 2 }).first
```
And then assign whatever new values you want to:
```
c.first_name = "Minerva"
```
And then don’t forget to save the changes:
```
c.save
```

#### DELETE
To delete a row, first find it:
```
c = Contact.where({ :id => 2 }).first
```
And then,
```
c.destroy
```
Intense.

## **➔ Creating tasks to populate helpful sample data** 

1. Navigate to `lib/tasks`, and create file to create your task, such as `lib/tasks/i_am_lazy.rake`

2. Place your task into the file,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fc8xkrpa88zwaoz2c7f.png) or any task you want to create. This one is creating a new contact for a contact book. 

3. In our terminal we will call `rake sample_contacts` and if you look at your database it should be populated!