NEWS: Cod free phentermine shipping Dacarbazine: Cheapest viagra online Crestor: Beconase Tetanus. Viagra prescription uk Sertraline Xanax detoxification Protonix Amoxapine Cyclizine: Phentermine mastercard Chlorpheniramine? Valium vs xanax Propofol Ambien eminem Phytonadione? Zoloft What do xanax look like! Protriptyline Buy generic phentermine! Watson soma Phentermine next day Viagra lowest price Thiopropazate Propofol Phentermine 37 5mg shipped to kentucky Ordering phentermine Pyridium Colchicine Aldactone Drug test tramadol hydochloride Cope? Ipratropium Bepridil Cialis in the uk Prozac and phentermine? Chlorambucil Plavix Cheap phentermine no shipping Keyword prescription qoclick tramadol without Can xanax cause frontal lobe dementia Nexium Riboflavin Cheap phentermine no rx. Cheapest phentermine 90 day order Premarin? Propylthiouracil Tramadol narcotic Discounted phentermine with no prescription Alesse! Cortisol Buy cialis in uk Xanax long term effects Colon cleanse ambien? Prazosin Viagra lowest prices Diethylstilbestrol Free shipping cheap phentermine Rated online pharmacies for phentermine Cheep paris france phentermine: Viagra discussion Tramadol medication, Phentermine prescription Strattera! Online pharmacy duromine viagra international How long does viagra last Phentermine prices Order phentermine online uk Does phentermine really work Cozaar Generic viagra overnight delivery Pantoprazole! Xanax on line Lorazepam Cheap phentermine no rx How does phentermine work: Cialis lowest price Griseofulvin? How fast will phentermine work Fda approved phentermine Oxacillin Generic sales viagra! Oxycontin xanax bars perclesept and lortab wha Cialis discount online Milrinone Butalbital fioricet, Levaquin Phentermine & health risks Phentermine hoodia Cleocin! Levitra vs cialis vs viagra Picture of xanax pill Diet hcl phentermine pill Differin Piperidolate Trovafloxacin. Prescription weight loss medication phentermine Pfizer viagra: Minocin Carbidopa Tramadol withdrawal Viagra online sales Hydrocodone info Next day delivery on phentermine. Aricept Cheapest viagra price. Prevacid Phentermine delivered overnight Adipex Snorting xanax Discount fioricet Exelon Glimepiride Acyclovir! Clarinex Phentermine 37.5 cash on delivery! Tessalon Provigil. Isoxsuprine Hydrocodone online pharmacy Concerta Pink oval pill 17 xanax identification Phentermine uk Cialis comparison levitra, Glycopyrrolate Free viagra order online! Buy phentermine on line Xanax drug test Tramadol cheap overnight inexpensive Prescription free viagra! Addiction recovery xanax Is klonopin stronger than xanax Tramadol 50mg Tolbutamide. Vicodin health Order soma online Tramadol 377 Nasonex! Viagra price compare Nitrofurantoin. Toprol Tyropanoate Buy meridia Pravachol Hyzaar Disulfiram Fda us approved phentermine Alternative herbal viagra, Buy viagra pill online Isosorbide Free sample prescription for viagra Hydrocodone apap 5 500 Triprolidine Comparison viagra cialis levivia Does phentermine help weight loss Actos: Viagra price list Compare viagra prices Cefaclor Xanax pills. Noroxin Adderall Cheap viagra in uk Amlodipine, Natural alternatives to viagra Hydrocodone for ibs: Hyzaar Information phentermine shortage Codeine How to discontinue the use of phentermine. Phentermine and urine drug screen Xanax anxiety No prescription phentermine Paroxetine. Estrone Azithromycin Lawsuits involving blindness caused by viagra Phentermine 37.5mg Dulcolax Do companies sell phentermine with low dose ingredients: Bontril phentermine adipex Fluticasone Cefuroxime Next day phentermine Cheap phentermine free consult Simethicone Delavirdine Half life of xanax Xanax vs klonopin Best price phentermine Phentermine buy best Buy discount phentermine? Cinoxacin Phentermine overnight No prior perscription tramadol Hydrocodone effects Does phentermine interact with hydrocodone Effexor Phentermine drug Herbal viagra for woman Meridia coupon Cialis levivia sales viagra, Phentermine tablet Buy xanax cod Woman take viagra Aminopterin Hydromorphone Xanax next day delivery Glucophage Per day buy phentermine, Stopping xanax Chlorhexidine Buy viagra canada Information phentermine Etoposide Buy viagra prescription online 180 tramadol Triamterene Lisinopril drug interaction viagra Cialis online sales Ticarcillin Allopurinol Cheapest tramadol online Carvedilol How to inject xanax pills Droperidol Hydrocodone vicodin Phentermine shipped to tn Reliable same or next day phentermine purchase online Natural phentermine Xanax information Buy viagra online cheap Discount phentermine no prescription Monopril. 37 effects phentermine side Fast delivery phentermine Diethylpropion Buy hydrocodone Phentermine order Cilexetil: Diazoxide Tobramycin Generic viagra in canada Donepezil Chlorhexidine Bar gold xanax Phentermine lowest price Weight loss phentermine! Buspirone Epivir. Xanax and valium Cheap phentermine no prescription? Dangers of phentermine Anxiety disorder xanax xr to wean off effexor xr Generic fioricet Overdose xanax. Viagra women Phentermine interactions Buy phentermine without prescription Phentermine use Hytrin Cialis comparison levivia viagra Cialis levitra viagra vs vs Vicodin prescription Viagra samples Xanax federal express Fosinopril Itraconazole Buy tramadol cheap Liothyronine Information phentermine Tramadol cause kidney problems Get viagra online Viagra and ischemic optic neuropathy Triamcinolone How long does xanax stay in your system, Side effects of viagra Cyclandelate Weight loss oral hcg and phentermine Buying viagra online uk, Cycrimine Cheap viagra canada Loxapine Buy cheap domain online atspace com xanax Iodamide Linkdomain buy online viagra info domain buy onlin Dichloralphenazone Thyroid. Paxil and xanax interaction How to get xanax Indomethacin Cialis impotence drug eli lilly co Phentermine side effects danger Addiction tramadol Natural over counter just like viagra stores Prozac drug interaction with xanax Generic viagra canada Cheap phentermine canada Chep phentermine Diatrizoate Tramadol ingredients Cheapest prescription viagra. Phentermine and carbs Carteolol? Buy no phentermine prescription Language phentermine ru Phentermine works Oxycontin xanax bars perclesept and lortab Dothiepin Lanoxin Does xanax show up on drug tests Xanax high Elavil Phentermine ingredients Iothalamate Methoxsalen Best generic viagra Androgel Is phentermine dangerous Exelon: Online pharmacy tramadol Add link phentermine purchase suggest Free trial viagra Viagra sample pack. Elidel Buy online viagra viagra Hydrochlorothiazide Carisoprodol Dexamethasone Actonel Troleandomycin Ordering xanax online Phentermine 37.5 tablet Ambien overnight. Lowest prices viagra Phentermine np with hoodia! Cheapest phentermine diet pills Order phentermine cod online Buy cialis in canada Digoxin Ceftizoxime Online ordering viagra Adipex phentermine vs Cheap phentermine with online consultation Online viagra Phentermine weight loss pills Phentermine reviews Enalapril! Generic viagra cialis Clarinex. Buying vicodin online Buying viagra online Chlorotrianisene Augmentin? Drug screening phentermine Fexofenadine Mycostatin Phentermine result Valium and xanax Xanax online cheap Cheap soma Cialis viagra Does viagra work for women Actos Detection drug in phentermine screen urine Herbal phentermine review Cheapest place to buy phentermine online Phentermine eprescriptions Itraconazole Hydrocodone cod only, Phentermine Viagra compared to levivia? Colistimethate Ultram Trifluoperazine Diet no phentermine pill prescription Fioricet Phentermine for weight loss! Dextrothyroxine Generic money order viagra Viagra herbal alternative Phentermine free shipping 90 supply no delay Free shipping phentermine Buy viagra without prescription Free overnight phentermine shipping Phentermine cod delivery, Famvir Phentermine guaranteed overnight shipping Cheapest place to buy phentermine Purchase tramadol online Butalbital fioricet Cialis effective soft tab treatment Order viagra buying viagra uk Lipids Phentermine florida Cialis compare levivia viagra: Viagra alternatives uk Levoxyl? Belladonna Buy viagra on line Phentermine lowest price 37.5 online Beconase Xanax picture Phentermine 37 5mg and mastercard, Phentermine 37.5mg tablet Soma cube? Female use viagra Buy discount viagra online, Cialis vs viagra Phentermine forum Viagra cialis comparison Losec: Methocarbamol Viagra herbal? Xanax overnight Xanax 1mg Free viagra online 50 hcl mg tramadol Phentermine canada Good morning viagra commercial Viagra dosages Difference between cialis and viagra Ditropan Buy viagra in canada Xanax dose Apcalis cialis. Oxybutynin Buy viagra online get prescription Good morning viagra commercial Viagra testimonials? Quinidine Soma bike? 25 mg viagra Hydrocodone order Xanax info Phentermine mastercard Generic name online qoclick tramadol Herbal viagra alternative review! Dangers of viagra Dioxyline Viagra for woman study Viagra shelf life? Order phentermine cod Celebrex Bromocriptine Cod phentermine shipped Tramadol side effects Esomeprazole Cod tramadol money orders Phentermine on sale Ipodate Xanax withdrawl symptoms Viagra cialis generic Lowest phentermine Cialis generic viagra Mesoridazine Fluphenazine Cialis compared to viagra! Hydrocodone overdose Perindopril Generic soft tab cialis Cheap hydrocodone Amiloride Midodrine. No prescription needed phentermine Mastercard phentermine Online cialis Purchase xanax online Detox hgh phentermine quit smoking xenical Mifepristone Dangers of phentermine heart Overnight shipping viagra: Locoid Injecting xanax. Tramadol narcotic Dicumarol Macrodantin Estrogen Xanax 2 mg Phentermine side effects dangers! Paris cheep phentermine No overnight prescription xanax Ritalin Reviparin Price viagra viacreme Effects of viagra On line prescription viagra Ethinyl 2 mg xanax Meridia vs. Phentermine. Viagra lowest prices Ship free viagra sample Risperdal Viagra erection Online pharmacies phentermine xenical meridia Viagra price compare Xanax grapefruit juice Nizoral. Rabeprazole Discount phentermine price Colace Klonopin versus xanax Cefadroxil On line viagra Discount phentermine online Buy viagra woman: Ordering 30mg phentermine Viagra in woman! Esmolol Alprazolam xanax Effects of viagra Is phentermine discontinued Phentermine without rx Cialis softtabs! Phentermine delivered cod Lynestrenol Natural viagra alternative Plicamycin Protirelin Biaxin. Phentermine feedback Nifedipine Buy online viagra viagra Online pharmacy prescription viagra Buying xanax Opium. Cialis compared to viagra What happens when women take viagra Purchase phentermine Phentermine pill. Sofia viagra Viagra cialis levivia Beclomethasone Celexa phentermine Diltiazem Ampicilin Diet drug fenfluramine phentermine Blindness viagra? Is xanax addictive Mixing cocaine and viagra? Moricizine Hydrocortisone? Vicodin drug test Cialis price, Ambien Purchase viagra online. Pulmonary hypertension and viagra Viagra sale online Lopressor Home made viagra Uk cheapest viagra Ambien cr dosage, Ionamin phentermine yellow Differin Ambien overdose Cheap tramadol online Griseofulvin Phentermine and carbs How long does phentermine stay in your system Viagra doseage. Metharbital Cialis dose Biperiden On line prescription viagra: Molindone Mixing viagra and cialis Adipex diet phentermine pill prescription Motrin Viagra cream Effect viagra Cheapest phentermine 90 day orders Amoxicillin Levivia and viagra Ranitidine Podofilox Diet ky phentermine pill ship that Xanax look alike Nabumetone Cialis generic Herbal phentermine does it work. Buy generic xanax Bricanyl Viagra in woman Phentermine pictures Fioricet information 100 phentermine Xanax liver damage Xanax versus prozac? Adipex ionamin phentermine Cycloserine Generic ambien Buy cheap generic viagra. Xanax prescription online Echothiophate Phentermine 37.5mg Clonidine Best price for generic viagra Buy cheap tramadol online Dicloxacillin Xanax online pharmacy no prescription Blue xanax Canada viagra! Dolasetron Prescription viagra? Medrol Dichlorphenamide Pink oval pill 17 xanax identification Buy tramadol without prescription Phentermine ups delivery Cialis com Phentermine withdrawal Ambien withdrawal Diclofenac Tolazamide, Cinnarizine Phentermine diet plan Procyclidine 37.5 phentermine! Phentermine for less Eon phentermine Miconazole Hydrocodone addiction! Cialis levitra sales viagra Purchase cialis: Carbimazole No online prescription xanax! Cheapest xanax online Cialis vs levitra Genaric viagra Aldara Phentermine np Carbamazepine, Buy phentermine cheap Phentermine and sibutramine be combined Soma seed Viagra substitutes Female spray viagra Liquid cialis: Allegra Levitra: What does xanax do Lansoprazole Lanoxin Furazolidone Diovan Phentermine phendimetrazine Prozac and xanax induced mood disorder Procyclidine. Celexa Viagrafix corporation: Cialis sales uk Lysodren Lowest price on phentermine Xanax grapefruit: Phentermine result Phentermine tablet

Rails Project Story: 1. Getting Started

So here is the first part of the previously announced Rails Project Story, in this part we are going to understand the Rails directory structure, design a very basic database to start with, link it to our rails application, setup the rails database schema for it and finally try the famous scaffolding feature that everybody talks about.

It seems that I underestimated the amount of work this tutorial is going to take that’s why I’m a day late, because of that I will change the weekday each part of this series will come up from Saturday to Sunday and sorry about this delay.

If you remember our project is BookSwap, a small system based on users, it will give them the chance to exchange and swap books between each others from their online bookshelves that they create. so let’s get started:

Normally I prefer to get something up and running as soon as I can, at least to enjoy seeing results from the very beginning of my project work and this is what we are going to do with this project as well, so to start let’s decide what is the simplest database design we can start with?

Database Schema

Basically from the light definition of our project we can tell that this project includes two main things: users and books, and from here we will go, we need two tables, one to hold the users data and we will call it Users and another one for books data and we will call it Books and now let’s discuss the relationship between these two tables

Obviously every user will have many books, and many users can own the same book (I mean the same title not the same physical book), so the relationship we have here is the many-to-many relationship which requires a third table in our database called users_books, therefore our database should look like this (db model is generated using DBDesigner4):

Users and Books tables

As you can tell from the database model above, I have mentioned the common and very basic columns of each table; fullname, login, email and salted_password for the Users table and title, author, publisher and ISBN for the Books table and naturally both of them have the default ID column as well.

We will use MySQL as the database server for this application, it’s time to create the three databases we will work with on our way:

bookswap_production: This will be the database we will use when we finish our development work and ready to have a live and completely working web application, that’s why it’s called the production database, it should contain the most stable version of our database structure with the most stable version of our code that’s why we separated it from the development database.

bookswap_developmentt: As you can tell, this is the database that we will play with during our development process, whenever we need to change anything or touch anything related to the database this is the one we should be dealing with in order to keep our production database in peace.

bookswap_test: In a sooner part of this tutorial we will cover the testing of our application, we will write code that will test our code and this testing code will deal with a completely separate database from both development and production databases, it will deal with the test database as it will require a lot of data insertions and deletion while testing which we want to keep away from other live and development databases.

If you have a GUI for your MySQL server you can create the above databases from there or we can simply use the command line admin tool:

D:\\Projects>mysqladmin -–user root -–password create bookswap_production
Enter password: ********

D:\\Projects>mysqladmin -–user root –-password create bookswap_development
Enter password: ********

D:\\Projects>mysqladmin -–user root -–password create bookswap_test
Enter password: ********

We will only create the databases now, we will not create any tables or relationships yet and actually we will do all that in Ruby and Rails after we get to know the directory structure of our rails application.

Generating The Rails Application

Before we get started with generating the rails application let’s just clarify few things, we are using Rails 1.1.6 for this application which got announced only few days ago (from the date of this post), so if you have Rails installed earlier before the update please upgrade before we proceed.

To generate our rails application we have to run the rails command, personally I have created a directory called “Book Swap” where I will store everything related to this project, including the rails app itself. So all I have to do now is to run the “rails” command in my directory with the project name to get my files generated.

D:\\Project\\Book Swap>rails bookswap
      exists
      create  app/controllers
      create  app/helpers
      create  app/models
      .
      .
      .
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

I guess you agree with me that these are a lot of files and directories that Rails has generated for us, maybe we should give them a quick scan and try to understand what they are for and what we will be dealing with more frequently:

File Structure

File Structure

app

This directory is the one that will contain most of our working files, it already contains four subdirectories (controllers, helpers, models and views), that is made to support the MVC nature of Rails, in brief the MVC methodology is based on separating our code into three levels:

First we have the Model which carries the core functions and responsible for all the data handling and manipulation, then comes the Controller which normally interacts with the model to retrieve data and prepare it to be viewed by the View which by its turn is the responsible of doing all the “show” business.

As for Helpers, they are the view supporters, when we reach the level that we have too much code in the view we better go and make a helper function and use it instead, once we start writing code we will get a better understanding of all these concepts which will happen very soon.

Behind The Scenes

All of these models, controllers and views are just the children of a bigger and more general parts of Rails, actually Rails is a union of different parts for each task which makes it more understandable and customizable to fit our best needs.

ActiveRecord is the part of Rails that’s responsible of interacting with the database and lets us deal with it as a complete object oriented solution, which really sounds like the model behavior (core functions and data manipulation) which is true, normally the model classes in our models directory are subclasses of ActiveRecord where they inherit the magic powers of it and get such strong tools to deal with the database.

“Active Record objects don’t specify their attributes directly, but rather infer them from the table definition with which they’re linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.” From Rails API

Following the same path Rails also depends on ActionController for its controllers, which is the midpoint between the views and the models, it receives the web requests from one side, activate the corresponding action which will contact the model for few data objects and then render the views to show the results.

“Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either render a template or redirect to another action. An action is defined as a public method on the controller, which will automatically be made accessible to the web-server through Rails Routes.” From Rails API

And finally we’ve got the views which are ActionView templates, we will see how they are just normal html, js or xml pages but with embedded Ruby code inside which also known as ERb, this embedded code helps us represent the data with some behavior and have a better separation between our design and our backend code.

“Action View templates can be written in three ways. If the template file has a +.rhtml+ extension then it uses a mixture of ERb (included in Ruby) and HTML. If the template file has a +.rxml+ extension then Jim Weirich’s Builder::XmlMarkup library is used. If the template file has a +.rjs+ extension then it will use ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.” From Rails API

config

From its name, config directory is where the configurations files are stored, the database link file, routs and environments files. database.yml is the database link file, where we will setup our database connection settings, environment.rb is the file where we specify our application environment, remember when we talked about development, production and test databases; each one of those is related to one separate environment as well that we can run our application on.

Config directory include environments subdirectory too which include environments configurations files, for each environment a different file, to control all the environments we have the father file we’ve just talked about environment.rb.

Finally comes the routes.rb file which (as it’s named) take care of all the web requests routing in our application, starting from some very basics routes up to a very complicated ones, it is very helpful for making clean and meaningful URLs as well.

components

In this directory we can create and store components which are something like a small application with model, controller and view that we can use all over our website instead of repeating ourselves for some modules.

db

From its name, the db directory will store our database related files, earlier we used to store the SQL files that generate our database structure but now it will hold only ruby files. It will store the schema and migrations files that we will discuss after a while in this part.

When we first generate our rails application the db folder will be empty but as we move along it will contain the schema.rb file that stores our database schema definitions and a subdirectory named “migrations” to store the versioned migrations we will create.

lib

Sometimes but not always there are some code that we want to write and it doesn’t fit under any of the MVC hierarchy, such custom code will be stored in the lib directory.

public

An interesting fact about Rails that none of these directories we are talking about is viewable or accessable online except for “public“, it contains the cgi/fcgi dispatchers that will do all the underground work to deal with the routes and our application, it also contains the images, stylesheets and javascripts subdirectories that we will be calling in our views and layouts.

script

You will notice when go a little further that rails is full of generators and embedded scripts that help us along the road to generate and create many things and even run a tiny web server as well the script directory is where all these stuff are hidden.

test

The test directory is where all the automotive tests are stored, there are functional tests for our controllers with some fixtures to load and a couple of unit tests for our models as well.

vendor

As in any application, sometimes we don’t want to reinvent the wheel, we don’t want to do something that’s already done, rails as an open source framework have many wonderful plugins and resources available that we can use in our applications and we will do that as well in BookSwap, all these external libraries and plugins will be stored in the vendor directory.

doc, log, and tmp

These three directories we don’t interact with their files much as they are automatically generated and updated, the log directory will store the log files of our web server for each environment, tmp will store the session, cache and socket temporary generated files and finally doc will store the html documentation that we will generate for our application (we will cover this up in some later part).

Link The Database

After we had this quick overview over the file structure in Rails we have to configure our rails application to connect to the databases we have created earlier, the responsible configuration for this is config/database.yml which normally looks like (notice that the databases names are exactly like the one we’ve created as they follow the project name):

# MySQL (default setup).  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql
# On MacOS X:
#   gem install mysql -- --include=/usr/local/lib
# On Windows:
#   There is no gem for Windows.  Install mysql.so from RubyForApache.
#   http://rubyforge.org/projects/rubyforapache
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql
  database: bookswap_development
  username: root
  password:
  host: localhost
 
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: bookswap_test
  username: root
  password:
  host: localhost
 
production:
  adapter: mysql
  database: bookswap_production
  username: root
  password:
  host: localhost

Now for the sake of DRY ‘ing our configuration file we will change it a little bit and add our database connection details as well to:

# MySQL (default setup).
defaultmysql: &defaultmysql
  adapter: mysql
  username: root
  password: secret #your password should be here
 
development:
  database: bookswap_development
  <<: *defaultmysql #calling the mysql defaults

test:
  database: bookswap_test
  <<: *defaultmysql 

production:
  database: bookswap_production
  <<: *defaultmysql

Basically what we have done is taking all the repeated information from the three database configurations parts, collect them under one reference and call this reference back in the configurations parts, by doing this we are not only DRY ‘ing our database configuration file but we also allowed different database engines to be defined more easily.

So our Rails application is linked to its blank databases using the MySQL adapter and it’s time to implement the structure we’ve decided on earlier using the rails schema and migrations facilities.

Rails + Database = Schema + Migrations

Migration in rails is the way Rails allow us to move back and forth in our application easily without losing any data from our databases but also keeping track of all the database structure changes from version to version (something like version control for our code files).

Database schema in Rails is very first database schema our rails application will have so if we want to install it on a new machine or load our database structure in any different database server it will be ready as a schema file that gets accumulated by the migrations files to move between versions.

In this part we will deal only with the schema part and later on with our versions and modifications we will get to know migrations better and have a closer look at them, now let’s generate the schema file and see how it looks like:

D:\\Projects\\Book Swap\\bookswap>rake db_schema_dump

This command will create a new file in our db folder named schema.rb, this file will hold all the starting schema setup and definitions and as this is a fresh application it will be empty from any definitions yet:

# This file is autogenerated. Instead of editing this file, please use the
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
 
ActiveRecord::Schema.define() do
 
end

Now let’s implement the basic database schema we agreed on at the beginning of this part, three tables with few fields, after the modification our schema.rb will be like this:

# This file is autogenerated. Instead of editing this file, please use the
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
 
ActiveRecord::Schema.define() do
 
  create_table "users" do |t|
    t.column "fullname", :string, :limit => 80
    t.column "login", :string, :limit => 80
    t.column "email", :string, :limit => 60
    t.column "salted_password", :string, :limit => 40
  end
 
  create_table "books" do |t|
    t.column "title", :string
    t.column "author", :string
    t.column "publisher", :string
    t.column "isbn", :string
  end
 
  create_table "users_books" do |t|
    t.column "user_id", :integer, :default => 0
    t.column "book_id", :integer, :default => 0
  end
 
end

If you read the above code you can easily understand that we simply created three database tables and identified their columns accordingly but notice also that we didn’t define the id column of the users and books tables, this is because ActiveRecord will create it by default.

create_table
is a simple method that generates SQL code of creating a table, we used its block form to add columns of our desired type, in the block form an instance of the table definition is created as “t” and when we call t.column it’s like if we say add_column in the regular form of the create_table method.

We have a schema file that contains our database structure, we have a rails application linked correctly to our database all we have to do now is to tell rails to import this schema to the linked database:

D:\\Projects\\Book Swap\\bookswap>rake db_schema_import
(in D:/Projects/Book Swap/bookswap)
-- create_table("users")
   -> 0.3440s
-- create_table("books")
   -> 0.1410s
-- create_table("users_books")
   -> 0.1720s

Running this command will import the database schema defined in schema.rb to our development database and that’s because the default environment of any new rails project is the development environment which can be changed and configured at anytime in config/environment.rb.

With this ruby-based database schema file we have saved ourselves from being attached to any database engine, Rails supports too many database engines like SQLite, Oracle, DB2, SQL Server and PostgreSQL, now our application can work with any of those by simply changing the connection adapter in config/database.yml file and import our schema to it using the above rake command.

This is one of the reasons why many designers or non-developers liked Ruby on Rails when they started to learn it as their first web development language and framework, Ruby has this beautiful self-explanatory code and Rails is full with Agile practices, it saved us from dealing with any SQL code and made our application cross-database-engines with Schema and Migrations.

All this read and all this steps and we didn’t see anything in action yet, maybe it’s time for us to try Scaffolding which is a quick auto-generator that we tell it about a table we want to scaffold and it will generate all the required Model, Controller and Views to connect with it and control it.

By running the following command we will be able to add, edit, delete and list all the data of our Books table:

D:\\Projects\\Book Swap\\bookswap>ruby script/generate scaffold Book Books
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/books
      exists  test/functional/
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/book.rb
      create    test/unit/book_test.rb
      create    test/fixtures/books.yml
      create  app/views/books/_form.rhtml
      create  app/views/books/list.rhtml
      create  app/views/books/show.rhtml
      create  app/views/books/new.rhtml
      create  app/views/books/edit.rhtml
      create  app/controllers/books_controller.rb
      create  test/functional/books_controller_test.rb
      create  app/helpers/books_helper.rb
      create  app/views/layouts/books.rhtml
      create  public/stylesheets/scaffold.css

This command will ask Ruby to run the scaffold generator to create the MVC setup for our table Books, the first parameter after “scaffold” is the Model/Table name and notice here that the table name here is not plural as we have specified it in our schema.rb, it’s Book not Books and this is because Rails will take care of this with it’s pluralization magic.

The second parameter in the scaffold command is the Controller/View name, basically it will create controller of the same name and a directory in the views directory named books with few templates for each action in the created controller.

Few other files got created too in the process, a helper and few test files, for now we will not deal with any of them but we can at least understand that for each data table of our database we can have all these structured files, a model to interact with the database and present the table data as objects, a controller which interact with these objects, filter them and prepare them to be viewed by the views which are templates with light embedded ruby code.

With this very minimum work, few commands and modifying a couple of files, we didn’t even write any code yet but we have something up and running and it’s time to have a sneak view on how it will look like.

D:\\Projects\\Book Swap\\bookswap>ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-08-20 16:49:04] INFO  WEBrick 1.3.1
[2006-08-20 16:49:04] INFO  ruby 1.8.4 (2005-12-24) [i386-mswin32]
[2006-08-20 16:49:04] INFO  WEBrick::HTTPServer#start: pid=5652 port=3000

We don’t even need to install a webserver while developing a rails application, we can simply try it with WEBrick, the simple http server bundled with rails. It will run by default on port 3000 on the localhost server so let’s check it out http://localhost:3000.

TestApp Rails Welcome Screen

Rails welcomes us, read this page as it has some useful information that we have discussed some and will discuss the rest (routes specifically) in depth later in other parts.

So where is the magic? We have generated a scaffold for the books table and we chose the controller/view name to be books, rails understand that and it will run everything automatically under the following mapping: servername:port/controller/action, in our case the servername is localhost running on port 3000, the controller we’ve created is named books but we don’t know any action yet but let’s try this: http://localhost:3000/books/

Book Listing

Great!! We have a page with the title “Listing Books”, an empty table with same headers of our columns names and finally a link that says “New book”. By clicking on this link we will see something more wonderful, a ready form, lovely and simple that allows us to add new books to our database.

New Book

Let’s try filling it out with some data and add the book, where will we go next? Back to the listing page but now it’s not empty like before, we have the new book we’ve just created listed there:

Added Book

What else have we got there? Three links in same row with our recently created book, Show, Edit and Destroy.. Do we have to explain? Clicking on Show will take us to a separate page that shows only the data of this particular row, Edit will take us to a form page loaded with the information of this record, ready to be edited while Destroy will popup a warning message before it will delete this record. This is it, one line only and the scaffold generator created all this for us.

Follow the URLs of each action and see how they are very meaningful and clean, all this web requests are handled in config/routes.rb as we said above, with the default route (mapping) as /:controller/:action/:id so for example if we go to http://localhost:3000/ books/show/2 routes.rb will forward this request to the books controller to run the show action on the book with 2 as its id.

Naturally scaffolding is not made to be used on its own, it’s made just to provide us with the very basics functions in a very simple way so we can understand how things work but we have to do the rest and modify the code to what perfectly suit our needs and that is what we will discuss in the next parts.

At The End

In this part we have met Rails in a little depth, had a quick look at the file structure it generates and understood some of the migrations and schema magic as well, technically we have now a basic rails application for books where we can add, edit and delete any record at any time after we wrote a very minimum amount of code and left the rest on Rails to take care of.

In the next part we will talk about creating some more tables, discussing migrations a little further and getting a better look with a nice interface and see how to implement the design with our Rails application as well, sorry for the length of this part and see you next week…

Comments

12

  1. Rida Al Barazi » Blog Archive » Rails Project Story said..

    [...] Getting Started: Basic Database design, implementing it through schema, introducing Rails file structure and finally scaffolding. [...]

  2. Rami Kayyali said..

    Thank you, thank you, thank you.

    That was one of the most comprehensive tutorials I’ve came across.

  3. kpumuk said..

    Looks like “Aglie web development with Rails” book. Will wait for next part of story, maybe something new will be in it.

  4. Ron said..

    I’ll be back next week, I’m working on a similar app — but not books. Looks like a good start, thanks!

    Ron

  5. Rida Al Barazi » Blog Archive » Rails Project Story: 2. User Authentication said..

    [...] Here comes the second part of our story, last time in part one we met rails directory structure, designed a three tables database, created it in MySQL and linked it to our application then we let rails db schema create those tables for us and finally we generated a modest scaffold for our Books table. [...]

  6. Kevin said..

    Rida,

    In the db schema file, how do you specify a particular type for a field? I want to speify a date type.

    Thanks,

    Kevin

    PS: Keep up the good work on the Rails project, I very much like it so far!

  7. Kevin said..

    I browsed the Rails api docs and figured it out. My problems was that I was using a limit for the :date type. Removed that, and everything worked fine.

  8. Rida said..

    Sorry Kevin for my late reply, I’m glad you found it yourself on Rails api, I was going to provide you a link there anyway ;)

  9. David said..

    Hi. Love the tutorial. Looking forward to the follow-up, as I’m stuck on a simple (?) concept: scaffolding for related tables.

    I have a “Team” and “Player” table for my football league. Players belong to teams, but I can’t figure out how to /player/create so that there is an auto link to a team.

  10. Omnikorn said..

    Excellent tutorial. I had been looking for a quick start in rails. Browsed many tutorials, but yours is the best.

  11. dimitrisd said..

    I must say that this first part, what i have read till now, is so good written that has meaning even for the complete rails beginner as i am.
    Thanks!

  12. Julia said..

    Thanks! Very interestingly.

Leave a Reply

you can use these xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Articles Permanent Link

is cialis or levitra better suggested dose in cialis what colors do valiums come in ex tramadol cialis drug levitra viagra phentermine with online docter consultation viagra news edinburgh comment search ambien no online pharmacy prescription us real valium phentermine for sale without perscription tramadol plus valium plus somas ultram home gym phentermine geberic viagra 50mg valium child custody drug generic generic viagra dose viagra cialis versus regalis valium while pregnant 37.5 card master phentermine amazing blonde fucked cialis order generic ambien 40mg dose of cialis cialis pharmacy direct viagra keyword viagra partial dose meridia online pharmacy phentermine umaxppc xenica viagra treat childhood pulmonary hypertension cialis and adverse effects information about cialis and livetra adipex online sales phentermine purchasing viagra effexor valium contradictions adipex phentermine treetop generic viagra us licensed online pharmacy viagra interaction with doxazosin da li viagra radi description of tramadol hcl-acetaminophen par overnight phentermine huge discounts online pharmacy viagra cialis levitra manufactures ptnrs searchfor viagra viagra impotence pill cialis usa mail crohns phentermine tramadol tramadol a a target blank mexico ambien viagra lawsuit settlements intel ambien modem drivers cialis side effect 0a aan cheapest shops selling phentermine phentermine diet online pharmacy viagra free sites find search pages pcp in urine valium tramadol next day delivery cheap phentermine withouta rx pfizer japan viagra insta phentermine lowest price for phentermine overnight cialis ingredient cialis get viagra pakistanian phentermine ambien on line fed ex german remedies cialis taking elavil and ambien viagra triangle cleveland ohio ambien and celexa drugs mailorder valium adipex meridia online phentermine prescription viagra cialis dizziness cialis liver problems hiv drugs and interactions with cialis weight loss philadelphia phentermine mixing valium with xanax cbs ambien bush fetches george porn viagra girl in viagra commercial gel tab viagra tramadol longterm use eon labs phentermine without a script tramadol meningitis buy phentermine adipex p 37.5mg phentermine no doctor roche valium no prescrption comparing cialis and levitra wine ambien price of valium on the street ambien shipped cod tramadol dosage for dog cheap price on phentermine viagra on-line tinnitus and ambien cr phentermine pharm in stock former senator who did viagra commercials phentermine tetracycline no prescription phentermine cod zanax valium overnight can you take viagra with wellbutrin order phentermine without physicians prescription viagra speedo videos phentermine no rx usa based sites zoloft tramadol interactions birth control allegra acid reflux cialis take phentermine with hydroxycitric acid pain relief tramadol buy viagra in new zealand valium cod buy phentermine cod pharmacy you never mix steroids with viagra ambien imitrex generic cialis tadalafil php ambien and anbien cr difference commview ambien 1low cost cialis get a prescription for viagra herbal ambien ambien cause depression viagra kamagra werkzame bestanddelen sildenafil natural urine detox for valium cialis levitra versus order phentermine adipex phentermine with online doctor los angeles viagra interaction flomax cialis sex story ambien cr 12.5 mg about phentermine best prices on phentermine buy adipex-p phentermine online cheap adipex phentermine tramadol valium together online valium without a prescription adipex diet discount phentermine pill uprima viagra cialis cheapest phentermine prices phentermine mg purchase health phentermine tablet cheap cheap phentermine viagra buy oonline zoloft viagra ejaculation zocor and ambien and interaction cialis levitra viagra comparisons tramadol 50 mg pliva 616 ingredients safe internet shopping generic viagra eng non-prescription valium phentermine with no physcian approval phentermine wihout rx sj lvmord tramadol phentermine and heart rate during exercise phentermine hcl 37 mg tablets drug interactions celebrex tramadol cyclobenzaprine ambien cr coupon about ultram tramadol viagra online money order save ambien cr on line tramadol and vicodin court lawsuits on viagra cialis tadalafil 20 mg viagra best prices fda approved phentermine for overnight and saturday delivery lisinopril viagra interaction valium without precription tadalis cialis tadalafil phentermine purchase phentermine tips tramadol order overnight saturday delivery price comparison for phentermine amp ultram combining metforman and phentermine generic valium and alert generic viagra louisville ky cialis vs viagra forums cheap valium without prescription viagra prescribing information 900 mg phentermine overdose ambien electronics compare cialis prices overseas ambien alternatives by cod tramadol discount phentermine online pharmacy cuba gooding jr cialis commercial 1000 tramadol 1buy cialis generic online generic viagra from canada phentermine no doctor prescription 4.33 n phentermine purchase 99 phentermine cheap viagra generic paypal similar medicines to phentermine phentermine tramadol viagra adipex low cost no prescription phentermine diet diet phentermine pill pill viagra online order cheap cialis for sale viagra damage after 4 hours order phentermine cod overnight delivery by effexor drug interactions with viagra ambien drug interaction ultram relief tramadol fda ambien toxicity buy tramadol online cod cialis free shipping cialis v s viagra phentermine usa pharmacy fda buy valium roche ambien cr review tramadol cicero phentermine pill photos no doctors prescription required phentermine search viagra viagra edinburgh pages online diet pills phentermine no prescription valium withdrawal effects effects of snorting phentermine multiple acts viagra order phentermine online without rx women s natural viagra better phentermine or adipex india phentermine does generic ambien work as well order phentermine no script ambien dwu attorney orange county ca get off of valium buy tramadol cod buy ultram cheapest mail order phentermine ambien used for chronic pain management viagra in kansas city phentermine drug testing tips online prescriptions for cialis boost as viagra phentermine sale 37.5mg express delivery generic viagra viagra news edinburgh tid cfm bad side effects of tramadol coreg cialis photo of ambien free cialis free levitra free viagra ambien generic drug cheap phentermine saturday delivery ups chep phentermine what does it looklike phentermine 30 photos valium 2mg pharmaceuticals viagra viagra epi cialis where cialis viagra softabs forced ejaculation male viagra viagra norvasc drug interaction bootleg pharmacy india phentermine pill my ebay bidding buy tramadol side affects on ambien 5mg overnight fedex ambien prescription celexa phentermine gt viagra help premature ejaculation phentermine success story gt ambien and lawsuit truth about ambien cr phentermine prescribe 15 cialis 20 mg viagra dosge phentermine 90 $149 phentermine wikipedia the c o d tramadol ship everywhere phentermine order generic meltabs viagra cialis drugstores viagra purchase on line tramadol and price generic ambien internet pharmacies phentermine c o d s too many ambien picture of viagra overnight cod phentermine caverta vs viagra phentermine 37.5mg online without rx phentermine problem side effects valium phentermine phentamine keyword viagra phentermine doctor san diego cheapest cialis erectile dysfunction pill price range for the drug viagra diet phentermine ephedra diet pills vitalbodyfitness cost low phentermine tramadol onlines buying ultram online tramadol total purchase phentermine shipped fedex buy valium online canada generic ambien pricing and managed care tramadol urine drug testing buy online us viagra viagra levitra cialis pharmacist perscription drugs online valium prescription take viagra who woman tamadol tramadol 180 pills phentermine hcl 30 mg actos phentermine target pharmacy 3 caverta veega generic viagra phentermine online purchas e penis enlargement pill viagra men order phentermine from pharmacy free consultation cialis commercial song cialis sale can i get viagra by internet tramadol order online cod cialis tadalafil contents arnold cialis phentermine and pulmonary hypertension instructions for valium tramadol hcl dosage phentermines sales ambien cr rebate form lowest phentermine 37 5 prices comparison cialis levitra viagra valium diarheah valium use in pregnancy tramadol famvir allegra cialis tramadol online tramadol hcl tramadol cheap long-term ambien use online order overnight tramadol interaction tramadol warfarin buy phentermine in kentucky questions about viagra find sites computer shop viagra search buy phentermine viagra meridia ultr order viagra air travel php viagra user review cod shipped tramadol phentermine phentremine phentermine aciphex aciphex phentermine actos risperdal little helper valium ambien chemical name generic cialis mexican when will viagra go generic phentermine s xanga site viagra liver problems cheap pharmaceutical viagra does the drug phentermine phentermine no prescription free consultation female free sample viagra prescription weight loss medications phentermine adipex cheap viagra cialis buy viagra online canada ionamin phentermine orn viagra generic for ambien cr no script next day phentermine valium grapefruit online sales viagra buying online risk viagra tramadol injection half life cialis levitra viagra comparison ambien sleep product discount viagra brand drug phentermine actos imitrex good deals on viagra flushed feeling and tramadol phentermine for cheap online pharmacy loss phentermine weight rss feed ambien recreational ambien alternative order viagra cialis levitra pharmacy seizures from tramadol cheap tramadol cod saturday delivery canada buy real viagra online phentermine with hoodia viagra commercial clips phentermine online diagnosis viagra best deals ambien login sleeping pills zolpidem ambien diet inexpensive phentermine pill zocor and avoid and ambien phentermine from germany buy levitra viagra phentermine health index pravachol phentermine skelaxin canadas viagra commercial familydoctor org sildenafil viagra phentermine $99 no script buy phentermine adipex p online phentermine overnight fedex no prescription 37.5 rainbowpush discussion board buy viagra 37 5mg phentermine pcp specialist thanks to ambien buy viagra without prescription side affects of viagra buy xanax valium viagra free sample valium and prozac cheap phentermine cheap phentermine online here cialis in uae k-9 tramadol hcl purchase valium c o d where can i buy viagra dogs and ambien online pharmacy no prescription required phentermine viagra vs levitra 37.5 90 phentermine drinking alcohol with tramadol viagra cialis buy no prescription buying phentermine online us licensed pharmacies phentermine erection add cardizem cd actos phentermine norvasc cialis tablet phentermine metabolized by pictures pills phentermine lose weight prozac phentermine cialis causes lower blood pressure viagra mice recall for ambien cr tadalafil vs generic viagra viagra levitra comparison phentermine to canada viagra cialis online pharmacy buy card debit online phentermine where can i order phentermine prices of viagra and cialis buy cheap generic viagra online tramadol and alcohol free viagra canada valium to treat canines phentermine foradil phentermine evista no prescription phentermine 15mg phentremine cialis and levitra compare viagra discount viagra prescription drugs viagra cialis canada consultation online pharmacy phentermine ambien mg doses will tramadol test positive for methadone effects of ambien on fetus phentermine cod pharmacy online viagra h omepage valium valium viagra love feelings order phentermine without calling doctor online pharmacy phentermine no prescription nasacort aciphex aciphex phentermine prescription pharmacy phentermine no prescription no doctor better levitra viagra viagra gift tramadol advil ambien heart disease cialis viagra propecia levitra erectile dysfunction cialis fast shipment cialis viagra online pharmacy tramadol crystal meth mexican prescription drugs names ultram tramadol 4.01 online phentermine purchase valium and purpose viagra 24 hours delivary discounts for phentermine cialis generic levitra review viagra phentermine review headaches when taking phentermine brown urine valium cialis fee online what is tramadol 3f no prescription valium overnight delivery detox hgh phentermine quit smoking xenical cialis cheapest lowest price phentermine chemical enhancement phentermine website impurity related substance viagra disounted phentermine canada buy viagra online valium gentic phentermine forums discussion phentermine no shipping to kentucky valium online consultation ambien books generic online phentermine pharmacy online order viagra online a href cheapest phentermine no presc tramadol side affect ambien for 49 95 phentermine cod mastercard viagra overseas information phentermine edinburgh uk viagra search cialis charles phentermine at cms ambien sirius commercial compare diethylpropion and phentermine valium look like order generic cialis c o d tadalafil cialis vs viagra tramadol and clonidine kill yourdelf with ambien zolpidem vs ambien cialis tadalafil treat where to buy phentermine without prescription fedex valium viagra in india tramadol cat medication buy phentermine blue white capsuls online cheap generic kamagra kamagra uk viagra ripoff phentermine websites long term effect ambien on liver viagra safe for dogs diazepam overdose valium overdose ambien insomnia side effects bbs bbs valiums viagra half life filing income tax buy tramadol valium and darvocet interaction rx cod valium usa cialis and levitra viagra made site viagra selges online rx phentermine tramadol halflife drugstore ambien order tramadol cod non phentermine prescription xenical hgh phentermine quit smoking grapefruit and cialis phentermine mexico ebay dding buy viagra viagra online diagnosis discover viagra best pharmacy phentermine tramadol hci black box warnings doctors prescribing phentermine online getting valium out of your system viagra ambien gift generic online pharmacy viagra phentermine and pharmacy will alcohol effect viagra dj valium omen 3 diet pill addiction phentermine viagra vs nitro 180 tramadol viagra instuctions tramadol trammadol metformin viagra comparison viagra levitra cialis discount drugs viagra 100mg cheapest ambien online generic phentermine pharmacy online dosage for tramadol valium information from drugs com phentermine where to buy 3 99 order ambien from canada phentermine detection tramadol best price oversea valium heavy sedation bournemouth buy from viagra take tramadol while bre