DEV Community

Brittany
Brittany

Posted on

Day 9: #100DaysofCode - ActiveRecord and a Database

Song of the day

Now that your database is set up , 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!

CREATE

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")
Enter fullscreen mode Exit fullscreen mode

.create

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")
Enter fullscreen mode Exit fullscreen mode

.new

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")
Enter fullscreen mode Exit fullscreen mode

and if you realize you do want to save that user then you would run

d.save 
Enter fullscreen mode Exit fullscreen mode

and TADA! the user with the username 'Derek' is now saved into the database!

.build

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
Enter fullscreen mode Exit fullscreen mode

You will get the following response:

#<Post:0x00007f9bb928c0d8 id: nil, title: nil, content: nil, user_id: 1>
Enter fullscreen mode Exit fullscreen mode

.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!'}])
Enter fullscreen mode Exit fullscreen mode

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>]
Enter fullscreen mode Exit fullscreen mode

READ

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.

.find

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)
Enter fullscreen mode Exit fullscreen mode

returns:

=> #<User:0x00007f9bb91b2b80 id: 4, username: "Derek">
Enter fullscreen mode Exit fullscreen mode

.find_by

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"
Enter fullscreen mode Exit fullscreen mode

It will return the following:

=> #<User:0x00007f9bb83abde8 id: 2, username: "Bob">
Enter fullscreen mode Exit fullscreen mode

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

Post.find_by content: "this is content"
Enter fullscreen mode Exit fullscreen mode

It will return the following:

=> #<Post:0x00007f9bb992c618 id: 1, title: "first title", content: "this is content", user_id: nil>
Enter fullscreen mode Exit fullscreen mode

.take

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)
Enter fullscreen mode Exit fullscreen mode

Will return:

=> [#<User:0x00007f9bb8e1e0f0 id: 1, username: "Ashley">, #<User:0x00007f9bb8e1e028 id: 2, username: "Bob">]
Enter fullscreen mode Exit fullscreen mode

.first

Returns the first element in your database

User.first
=> #<User:0x00007f9bb994cad0 id: 1, username: "John">
Enter fullscreen mode Exit fullscreen mode

.last

Returns the last element in your database

=> #<User:0x00007f9bb83e2b18 id: 4, username: "Ashley">
Enter fullscreen mode Exit fullscreen mode

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.
Check out the active record documentation here

#100DaysofCode Github Repo

Top comments (0)