Tables are great for sitting at and even better for organizing your data. Let's take a look at how to do one of the first tasks you'll want to do with Ruby, creating tables. We'll be backend experts in no time!
A great way to create your initial tables/database in Ruby is Active Record, a Ruby gem creates a link between Ruby and databases. You can read more about Active Record basics on its official guide if you'd like, but no need to right now for this guide.
Active Record is a Ruby gem, so an entire library of code that you can install by using this command in the terminal of your ruby project location: gem install activerecord
Alongside Active Record, we'll be using a tool command called 'rake' which you can install using the console command gem install rake
One last bit of information up front: We'll start to make our database using something called migrations. A migration creates a schema Ruby file, that we can view that contains all of our data in our database and creates a new 'version' of the database with each migration we make. Active Record will create a db/schema.rb file after we create our first migration and update it to match the up-to-date structure of your database, ensuring that it is all set up correctly.
Now comes the fun part! Let's make a database table for cats, since I found a free picture of a cat on a table for this article.
To start, within our Ruby project we'll run console commands to begin our two migrations. The command to create a migration is:
bundle exec rake db:create_migration NAME=create_insertnames
So in our case, it would be
bundle exec rake db:create_migration NAME=create_cats
This will create a new Ruby Class file in our project. You'll notice that they have a long string of numbers that signify when the migration was created (which is useful for version control), followed by _create_cats. Awesome!
Within the file note how the class inherits from Active Migration.
class CreateCats < ActiveRecord::Migration[6.1]
This allows us to run commands without having to code all of them within each file. It's a huge timesaver.
Within our CreateCats class file, we'll want to create the framework for our table, which we can do, like so:
class CreateCats < ActiveRecord::Migration[6.1]
def change
create_table :cats do |t|
t.string :name
t.string :color
t.integer :age
t.string :type_of_table
end
end
end
This table would have 4 columns, one for the each cat's name, color, age and the type of table that they are on. Note how we label what kind of data will be in each cell of the table. Text is represented as string, with numbers represented as an integer. There are some other types you can research, but these are your two most common types. Note: be sure to add a new 'end' after you create your table!
Now that you have the framework for your table, you can migrate it to update your schema with the command bundle exec rake db:migrate
Your schema will now update to reflect your current table code and will continue to update itself to include any additional classes that you migrate, so you can create more tables and really get a database going!
To view the table that we are creating, you can use the Visual Studio Code (VS Code) extension SQLite Explorer. This can be found in the extensions search within VS Code and once installed you can start it by clicking on your schema, then using the key command ctrl+shift+p. This will bring down a dropdown menu that will allow you to click on your file and open a new SQLite Explorer dropdown on the lower left-hand of your VS Code screen that you can click to find and open your table.
If you look at your cats table, you will see that it exits, but is blank:
Let's use the terminal to create some cats! To create Cat class objects, let's make one more Ruby file in the project called Cat.rb. It will also inherit ActiveRecord::Base like so:
class Cat < ActiveRecord::Base
end
Next, let's make the Cat objects! Type rake console
in your terminal which opens a pry console where you can edit and test your Ruby code from within the terminal.
Once you're in the pry session, create some new cat objects with the create method inherited from Active Record. For example: Cat.create(name:"Frank", color: "black and white", age: 8, type_of_table: "oak")
After creating a few more cats we will end up with a table we can view in SQLite Explorer in VS Code that looks like this:
Boom! We have a table of cats on tables! Keep coding and who knows what kinds of exciting databases full of cats (or other stuff, I guess, if you're into that) you can create. Good luck coding; I'm excited to see what you make.
Top comments (0)