My Journey with Active Record and Ruby
Last week — week 3 of our 12 week Iron Yard course we tackled Active Record. It was different that anything I’ve seen before. I struggled with it quite a bit. There are so many parts to understand and there are so many things happening “magically” which is great — when you know all the conventions and know exactly how to do things so that everything talks nicely and gives me the information I want. I’ve compiled my notes and hopefully this will help me solidify my understanding as well as create a reference for me for when I need to do this again. Apparently, we will take a break from this for a couple weeks and then come back. I want to have notes to come back to that will remind me of all that we have learned.
Creating the ActiveRecord database consists of 4 steps:
- Generate Migration
- Fill out Migration
- Run the Migration
- Create a Model
So I am a total newbie and I understand that when we get to Rails this will be a part of Rails but for now I only know what I know. So here’s how we are doing it to create a new table:
- To Generate the Migration
$rake generate:migration TableName
The TableName doesn’t matter, you want it to be unique so don’t name it so it doesn’t conflict (I made that mistake once when I was updating a table and I named it UpdateTable twice — you can learn a lot from your mistakes!).
2. Fill out the Migration — Generating the migration creates a file, with a date timestamp in the name of the file in the migrate folder. Open that file and add to the class that has been created for you. If you are creating a table it will look something like:
class CreateUsers < ActiveRecord::Migration
create_table "users" do |t|
where “users” is the name of the table, and “name”, “wins” and “losses” are names of the columns.
3. Run the Migration
4. Create a Model — in the lib folder create a ruby file, the name of the file should be the same as the table name but lower case and singular. So a table users should have a Class User inside a user.rb file. The naming convention has gotten the better of me a time or two. It’s super easy for an example like this but gets more complicated when you have a two word name or underscore in the name or for a join table. I have gotten mine working but I don’t feel like I know enough to clearly explain that — maybe that’s a blog post for another day. So — for today in your user.rb file you will add your validations as well as create your class to inherit from ActiveRecord::Base. If you don’t have anything to put in the model just create an empty class, but if you know your validations and relations you can put them in now too. Two examples of Models I’ve created :
class Item < ActiveRecord::Base
validates :todo, presence: true, uniqueness: true
class User < ActiveRecord::Base
I should mention that it is way easier if you plan ahead and know the names of the tables you are going to create. It is possible to delete a table, add columns, etc but that is more difficult than creating the right table in the first place.