microtuts

Create Some Models and Get the Data Flowing With Rails 3.2

Things that might matter
Ruby: 1.9.3
Rails: 3.2
OS: MacOS X Snow Leopard

We have a fresh Rails project and want to play with some data right meow. Let’s just DO IT.

First of take a quick peak at config/database.yaml. It should look like this:

So here we have 3 environments defined. The default database is sqllite3. This database reside as a simple file in our project. We can later switch this to MySQL (among other things) but for now let’s just rock it like this. You can see the development database file at db/development.sqlite3.

We’re going to generate a model first and then have Rails create the db structure for us. In the terminal type:

“Idea” is the table name. We then go on the define three fields. I’ve named the table “Idea” because I’m trying to reconstruct a site I built on Codeigniter for sharing ideas for stupid things to do with your time (www.thirtydogs.com).

Now we can see a bunch of stuff has been created for us…

      invoke  active_record
      create    db/migrate/20120413221250_create_ideas.rb
      create    app/models/idea.rb
      invoke    test_unit
      create      test/unit/idea_test.rb
      create      test/fixtures/ideas.yml
       route  resources :ideas
      invoke  scaffold_controller
      create    app/controllers/ideas_controller.rb
-- continued --

Now I remember seeing Rails demos where people would jump right into the console and start interacting with their models. I like that idea so let’s try it. Jump into the Rails console:

rails c

I have no idea what I’m doing so I type “Idea” to see what happens:

1.9.3p125 :001 > Idea

That doesn’t quite work but gives me a hint…

1.9.3p125 :001 > Idea
 => Idea(Table doesn't exist)

Looking into it I find that the scaffolding has created some database migration files that will build the db for us but hasn’t run them yet. Take a look at the file at db/migrations/20120413221250_create_ideas.rb.

We can run the migration by entering the following command in the terminal:

rake db:migrate

If all goes according to plan we should now have a table in our database. In order to open a SQLLite database I downloaded and used a trial of Base for Mac. Here’s what I get:

Good stuff. It seems the t.timestamps line in our migration script lead to the creation of the columns “created_at” and “updated_at”. Pretty cool.

Now let’s enter the rails console again by into the terminal:

rails c

We’re going to try and create and object and the commit it to our database. We can create an Idea object using our Idea class like so:

The console should return the following:

We now have an object called “idea” floating around in memory. If we type idea into the console we’ll see the same output as above:

If we check our database we’ll see that there is still no data in our Ideas table. In order to commit this object to the database we need to enter:

As you can see Rails generated and INSERT query for us and filled in the blanks appropriately.

If I check the database I can see that the record was successfully inserted and our “created_at” and “updated_at” columns have been populated appropriately as well.

I’m happy with that for now. Next I might look into switching to a MySql database. I’ll probably also play with some data relationships and validation. Heck, I might even see what kind of image handling we can do in this framework. Take care.


Creating your first programming language is easier than you think.

Understanding how a programming language works is a surefire way to be a better developer. Read more.


Suggested Reading


written by Ryan Bosinger
Ryan is a web developer living and working in Banff Alberta. He also writes for giftsforpeoplewitheverything.com and freefoley.com.

Leave a Reply