Day 9: #100DaysofCode - ActiveRecord and a Database
Tags: ruby, womenintech, codenewbie, 100daysofcode
Published: 2020-06-10 03:07:02

<a href="https://soundcloud.com/neo-soulfood/sepsenahki-let-it-go?in=user-265914316/sets/neo-soul"> Song of the day </a>

Now that your database is <a href="https://dev.to/sincerelybrittany/day-8-100daysofcode-setting-up-sinatra-database-part-4-9d6"> set up</a> , you should be able to use active record to Create, Read, Update, and Delete data in your database. Today we will review the first two examples of CRUD, CREATING AND READING your database! 

<h3> CREATE </h3>
Lets start with CREATE, run ``rake console`` and create some users:
```
a = User.create(username: username= "Ashley")
b = User.create(username: username= "Bob")
c = User.create(username: username= "Carla")
```
<h4> .create </h4>
The ``.create``, creates a new object inside of your database and automatically saves it. 

Lets also create one post:

```
p = Post.create(title: title= "first title", content: content="this is content")
```

<h4> .new </h4>
But what if you wanted to create an object without automatically saving it?

Then you would run ``.new`` like this:

```
d = User.new(username: username= "Derek")
```
and if you realize you do want to save that user then you would run

```
d.save 
```
and TADA! the user with the username 'Derek' is now saved into the database!

<h4> .build </h4>
Another "create" option you should know is ``build``. When you have a has_many relationship, like in our example, we may need to build a user with posts, like so:

```
a.posts.build
```

You will get the following response: 
```
#<Post:0x00007f9bb928c0d8 id: nil, title: nil, content: nil, user_id: 1>
```

``.build`` will allow you to create posts/content to a single user without saving the post.

For example: 

```
a.posts.build([{content: 'You are'}, {content: 'Creating'}, {content: 'New Content!'}])
```
You will get the following results:

```
=> [#<Post:0x00007f9bb82381c8 id: nil, title: nil, content: "You are", user_id: 1>,
 #<Post:0x00007f9bb8240990 id: nil, title: nil, content: "Creating", user_id: 1>,
 #<Post:0x00007f9bb8249040 id: nil, title: nil, content: "New Content!", user_id: 1>]
```

<h3> READ </h3>
Now that we can create and add data to our database, how do we read it? ``find``, ``find_by``, and ``take`` will become your best friends in ActiveRecord. 

<h4> .find </h4>

This ``.find`` method allow you to find an item based off the unique id that is automatically generated when you create and save an object to the database. For example, if you wanted to find 'Derek' you would use the id of 4 since it was the fourth item created and saved to the User database. 

```
User.find(4)
```

returns: 
```
=> #<User:0x00007f9bb91b2b80 id: 4, username: "Derek">
```

<h4> .find_by </h4>
Allows you to search for an object with certain conditions, usually like a name or string. For example, let's say you wanted to find the user with the username Bob. 

```
User.find_by username: "Bob"
```
It will return the following:

```
=> #<User:0x00007f9bb83abde8 id: 2, username: "Bob">
```

You could do the same thing to find content like so:

```
Post.find_by content: "this is content"
```
It will return the following: 

```
=> #<Post:0x00007f9bb992c618 id: 1, title: "first title", content: "this is content", user_id: nil>
```

<h4> .take </h4>
Will get a record for you without implicit ordering, for example, what if you wanted to get the first two items in your database, you could use take and specify the number of items you want, like this: 

```
User.take(2)
```

Will return: 
```
=> [#<User:0x00007f9bb8e1e0f0 id: 1, username: "Ashley">, #<User:0x00007f9bb8e1e028 id: 2, username: "Bob">]
```

<h4> .first </h4>
Returns the first element in your database

```
User.first
=> #<User:0x00007f9bb994cad0 id: 1, username: "John">
```

<h4> .last </h4>
Returns the last element in your database

```User.last
=> #<User:0x00007f9bb83e2b18 id: 4, username: "Ashley">
```

There are many other methods that are available to you through Active Record and I recommend learning and using as many as you can to get the hang of creating and reading your database. 
<a href="https://guides.rubyonrails.org/active_record_querying.html"> Check out the active record documentation here </a>


<a href="https://github.com/SincerelyBrittany/100DaysofCode"> #100DaysofCode Github Repo </a>