Doug's Web Development Blog

web development with Ruby on Rails and more.

Tealeaf - Rapid Prototyping With Ruby on Rails Cousre Done!

| Comments

It’s been a few weeks since I last posted about my journey to becoming a Ruby on Rails developer through Tealeaf Academy . At this point I’m done with their second course “Rapid Prototyping with Ruby on Rails” . This blog post is actually my final assignment for the course. I have learned to build a basic rails app from start to finish and I’ve even been able to apply some custom features that weren’t discussed in the course.

Here is the final project http://shielded-retreat-9536.herokuapp.com Here is my code on GitHub - https://github.com/doug7410/postit-app

I’ve learned way to much to go into detail about everything so here is a summery of all the things I learned while building this rails application.

  • Model View Controller
  • Rails Migrations
  • Active Record
  • Active Record Associations
    • 1 to Many Associations
    • Many to Many associations
    • Polymorphic associations
  • RESTful routes
  • Custom Routes
  • ERB templates
  • User Authentication with “has_secure_password”
  • Helper Methods
  • Modules
  • Creating a Gem
  • Rails flavored AJAX

I’m probably leaving some things out, so I might have to go back and edit that list. In my last post I talked about MVC and Rails Migrations. Now I’ll talk a little about Active Record and Associations .

What is Active Record?

Active record is an ORM (Object Relational Mapping system).  Active Record allows Rails to interact with a database and and also stores information about the relationships between tables in the database. All with out ever writing SQL.This is really incredible if you ever had to write SQL to interact with a database! Active Record completely eliminates that whole layer of thinking.

Here’s a simple example of the power and convenience of Active Record. Here is a simple table and it’s model.

dogs table(tables are always named in plural form)

ID Name Weight Age
1 Rocky 15 5
2 Gump 45 11
3 Jenny 35 9

Dog model dog.rb (model names are always singular)

1
2
class Dog < ActiveRecord::Base
end

Notice the Dog class is completely empty aside from the fact that it inherits from ActiveRecord::Base. The Dog class now has access to all the Active Record methods. If we wanted to select a row form the dogs table you can do so like this in rails console.

1
2
irb> dog = Dog.first
=> <Dog id: 1, name: "Rocky", weight: "15", age: "5">

This would return the first record in the ‘dogs’ table. Active record has many other methods for returning rows form a table. Let’s say we wan’t the dog with an ID of 2

1
2
irb> dog = Dog.find(2)
=> <Dog id: 2, name: "Gump", weight: "45", age: "11">

Active Record also creates getter and setter methods for every column in the database table. So you can do things like this.

1
2
3
4
5
6
7
8
irb> dog.name
=> "Gump"
irb> dog.age
=> "11"
irb> dog.age = 12
=> 12
irb> dog
=> [id: 2, name: "Gump", weight: "45", age: "12"]

Active record makes working with data a breeze. If we had to write SQL just to do the few examples I have above it would take way more code and would be way less pretty.

The other great thing about Active Record is Associations

Associations are used to create relationships between tables. There are 2 ways tables can be related

  • 1 to Many
  • Many to Many

Here is another table that can relate to the dogs table:

owners

ID Name
1 Frank
2 Rachel

In this example we’re going to say a dog has one owner, and an owner can have many dogs. This a a typical one to many relationship. The Dog and Owner models will look like this.

Dog model

1
2
3
class Dog < ActiveRecord::Base
  belongs_to :owner
end

Owner model

1
2
3
class Owner < ActiveRecord::Base
  has_many :dogs
end

Doing this will require one column to be added to the dogs table. A foreign key of owner_id

dogs table

ID Name Weight Age owner_id
1 Rocky 15 5 1
2 Gump 45 11 1
3 Jenny 35 9 2

Rails will automatically know to use owner_id because we stated in the Dog model that a dog belongs_to :owner . The foreign key column always goes in the table on the belongs_to side of a relation.

Now we can do things like this.

1
2
3
4
5
6
7
8
irb> dog = Dog.find(2)
=> <Dog id: 2, name: "Gump", weight: "45", age: "12">
irb> dog.owner.name
=> "Frank"
irb> owner = Owner.last
=> <Owner id: 2, name: "Rachel">
irb> owner.dogs.first.name
=> "Jenny"

So that’s a little taste of what active record can do and how classes can be associated.

Here is a great reference for all Active Record - http://guides.rubyonrails.org/active_record_basics.html

Rails uses a lot of conventions to make all of this magic work, but if you can remember them you can be very productive!

Comments