-
-
Save ryandhaase/4bd10038b1fa908e0c6b to your computer and use it in GitHub Desktop.
How to Create, Read, Update, and Delete - Hashes in a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #This is text version of the video/slides from the first checkpoint in railsforzombies.org/levels/1 | |
| #Hash Structure (variable, key, values) | |
| b = { | |
| id: 3, | |
| status: "I just ate some delicious brains", | |
| zombie: "Jim" | |
| } | |
| #how to retrieve a value from the hash: | |
| variable[:key] => value | |
| b[:status] => "I just ate some delicious brains" | |
| b[:zombie] + " said " + b[:status] => "Jim said I just ate some delicious brains" | |
| =begin | |
| Accessing tables | |
| tweets | |
| id status zombie | |
| 1 Where can I get a good bite to eat? Ash | |
| 2 My left arm is missing, but I dont care. Bob | |
| 3 I just ate some delicious brains. Jim | |
| 4 OMG, my fingers turned green. #FML Ash | |
| =end | |
| t = Tweet.find(3) | |
| #The capitalized and singular Tweer allows us to access the lowercase | |
| #and plural tweets table name. The .find is a method that allows us to access the id of 3 | |
| #With your find(3) method stored in t you can now use the below code to access that hash. | |
| #hash syntax dot syntax | |
| puts t[:id] puts t.id | |
| puts t[:status] puts t.status | |
| puts t[:zombie] puts t.zombie | |
| #CRUD | |
| #Create: creating a new tweet | |
| t = TableName.new | |
| t.key = value | |
| t.save | |
| t = Tweet.new | |
| t.status = "I <3 brains." | |
| t.save | |
| # or | |
| t = TableName.new(hash) | |
| t.save | |
| t = Tweet.new( | |
| status: "I <# brains", | |
| zombie: "Jim") | |
| t.save | |
| # or | |
| TableName.create(hash) | |
| Tweet.create(status: "I <3 brains", zombie: "Jim") | |
| #Read: read a tweet from the table | |
| Tweet.find(2) | |
| Tweet.find(3, 4, 5) | |
| Tweet.first # => returns the first tweet | |
| Tweet.last | |
| Tweet.all | |
| Tweet.count # => returns total number of tweets | |
| Tweet.order(:zombie) # => returns all tweets, ordered by zombies | |
| Tweet.limit(10) # => Returns the first 10 tweets | |
| Tweet.where(zombie: "ash") # => returns all tweets from zombie named 'ash' | |
| Tweet.where(zombie: "ash").order(:status).limit(10) # => returns only tweets from zombie 'ash', ordered by status, and only the first 10 | |
| #Update: how to update a value within a hash withing the table tweets | |
| t = TableName.find(id) | |
| t.key = value | |
| t.save | |
| t = Tweet.find(3) | |
| t.zombie = "EyeballChomper" | |
| t.save | |
| # or | |
| t = TableName.find(id) | |
| t.attributes = hash | |
| t.save | |
| t = Tweet.find(2) | |
| t.attributes = { | |
| status: "Can I munch your eyeballs?", | |
| zombie: "EyeballChomper" } | |
| t.save | |
| #or | |
| t = TableName.find(id) | |
| t = TableName.update(hash) | |
| t = Tweet.find(2) | |
| t.update( | |
| status: "Can I munch your eyeballs?", | |
| zombie: "EyeballChomper") | |
| #Delete: how to delete a zombie from the table 'tweets' | |
| t = Table.find(id) | |
| t.destroy | |
| t = Tweet.find(2) | |
| t.destroy | |
| #or | |
| TableName.find(id).destroy | |
| Tweet.find(2).destroy | |
| #or to destroy all zombies | |
| TableName.destroy_all | |
| Tweet.destroy_all | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment