loading...

Day 9: #100DaysofCode - ActiveRecord and a Database

sincerelybrittany profile image Brittany ・3 min read

#100DaysofCode (72 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 70 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables 66) Day 66 : #100DaysofCode - Creating a Follow/Unfollow in Rails 67) Day 67 - #100DaysofCode - Adding Categories to Rails App 68) Day 68 - #100DaysofCode - Making Sure Only Admin Can CRUD Application - W/O CanCanCan 69) Day 69 : #100DaysofCode - Coming Up With An App Idea 70) Day 70 : #100DaysofCode - Taking A Break And Learning From Others Is Important 71) Day 71 : #100DaysofCode - Re-route rails app after log in with devise 72) Day 72 : #100DaysofCode - Still Trying To Get API To Work

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")

.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")

.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")

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!

.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

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>]

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)

returns:

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

.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"

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>

.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)

Will return:

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

.first

Returns the first element in your database

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

.last

Returns the last element in your database

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

#100DaysofCode Github Repo

#100DaysofCode (72 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 70 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables 66) Day 66 : #100DaysofCode - Creating a Follow/Unfollow in Rails 67) Day 67 - #100DaysofCode - Adding Categories to Rails App 68) Day 68 - #100DaysofCode - Making Sure Only Admin Can CRUD Application - W/O CanCanCan 69) Day 69 : #100DaysofCode - Coming Up With An App Idea 70) Day 70 : #100DaysofCode - Taking A Break And Learning From Others Is Important 71) Day 71 : #100DaysofCode - Re-route rails app after log in with devise 72) Day 72 : #100DaysofCode - Still Trying To Get API To Work

Posted on by:

sincerelybrittany profile

Brittany

@sincerelybrittany

Web Developer | Software Engineer πŸ‘©πŸΎβ€πŸ’» | Determined | Music & Dance | #100DaysofCode | #WomenWhoCode

Discussion

markdown guide