Rails101 Tutorial Ruby on Rails with Relations

Blog ยป Rails101 Tutorial Ruby on Rails with Relations

Posted on 11 May 2009 21:39

Here comes to tutorial to enable you to create rapid Ruby on Rails applications with relations and validations. This article is in introduction Rails101.

This tutorial is depreciated. It won't be upgraded to Rails 3. I am using Hobo instead of ActiveScaffold and rails templates now.

I suggest all-about-testing-hobo-organizations-revisited which covers my testing experience on the simplest Hobo application you can build.

This tutorial is similar to Ruby on Rails with One Model, however, we do a bit of Rails programming here to associate the models. And it's not the continuation of the project started there. This is different.

If you don't have ruby then do * Basic Ruby on Rails Installation first.

First, create the application1. We shall use the rails template http://tinyurl.com/cs-active-scaffold to initialize the project

rails addresses -m http://tinyurl.com/cs-active-scaffold
sudo chown $ME:$ME -R addresses/public

Let's go into the application:

cd addresses

Only on Windows:23

rake gems:install

And create our first model45:

script/generate cs_active_scaffold Person first_name:string last_name:string birth_date:date
script/generate cs_active_scaffold Address person_id:integer street:string city:string zip:string country:string
rake db:migrate

Yes, addresses point to their person. But Rails do not make an assumption about this so

$RAILS_ROOT/app/models/person.rb should be:

class Person < ActiveRecord::Base
  has_many :addresses, :dependent => :delete_all
  validates_presence_of :last_name
  validates_presence_of :birth_date
  def to_label

All readable right? "to_label" is for the views.

$RAILS_ROOT/app/models/address.rb should be:

class Address < ActiveRecord::Base
  belongs_to :person
  validates_presence_of :city
  validates_presence_of :zip
  validates_presence_of :country
  def to_label

At this point edit "app/views/cs_global/_application_menu.html.haml" and make it look like

    = link_to "People", people_path
    = link_to "Check CSS", :action=>'check_css', :controller=>'info'
    = link_to "About Rails", '/about_rails.html'

Becareful, no tabs. All identation must be spaces (multiples of 2 spaces). This is an HAML file which happens to be more readable and powerful then HTML.

Let's start the server


Now you can use the database

This application uses haml and sass to define the general view layout.
ActiveScaffold makes the AJAX based database navigation. You are not limited by that. You can customize each detail or throw it away completely.
Validators like 'validates_presence' keep your data consistent. Read more about them or check the official API.

Now you are able to specify one-to-many relations. You can read more about many-to-many relations.
With this much you can do most RAD databases, I guess.

Uh, in Rails we don't go around with ground up programmings, most of the things you would like to do are available as plugins or gems. See what gems are out there:

gem list --remote

Some advice

You can not learn everything at once. But you can in order while developing an application.

  • Keep your application populated with data so that you can see what happens quickly. Write fixtures to populate your development environment. See ruby-on-rails-with-one-model for an example of using fixtures in development:
rake db:fixtures:load
  • Complete your database model. And be prepared to do upgrades after your first deployment. So learn Rails Migrations very well. Complete your validations.
  • Then improve how your data is visualized. Improve your database UI. For that study ActiveScaffold
  • Start to customize the UI frame surrounding the data navigation. Or customize styles. Learn how to edit HAML files around the application. Edit SASS files around the application to improve the color and layout of things. Use FireBug to understand the styling of the pages.
  • Finally you can add Authentication with Clearance to your application.

What is next?

As you have the ability now to create a project instantly and wire your relations, you can dive into the further details of ruby and rails at your leisure:

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License