
Ruby rails - dig little deeper
12/27/2009 10:13 pm By Sameera Gayan | Articles: 9
OK, I hope you have red my previous articles about creating a simple blog in ruby and rails (http://digit.lk/node/440 and http://digit.lk/09_dec_ruby) . There we jumped in to rails directly and created a simple yet fully functional blog. Today we will go through some theories behind what we did , because its always good to understand the underlying functionality of what you are doing..
But believe me it’s not hard to understand, after all its Rails!!!!
Before start 3 main key points
- Rails is a web framework (implemented using ruby programming language)
- Rails / ruby is open source
- Rails makes developer lives easy :D
OK lets start,
Rails is a web framework
Rails is a web framework, don’t miss understand that rails is ruby. Ruby is the programming language which implemented rails framework. OK, now what is a framework, simple explanation would be framework is a structured way of organizing things , each object has its own place and common objects are grouped together . (if you need more heavy weight answer - http://en.wikipedia.org/wiki/Software_framework)
Web framework is as same as above. When it comes to rails, rails has a defined directory structure (each object has its own place..) because of that developers know which functionality should be implemented where (..common objects are grouped together..) Following is a typical rails directory structure

em>Figure 1.0
Rails follows MVC architecture (Model, View, Controller) , think of it as this way , As developers you
must have created class diagrams, classes methods, etc… So all your OO goes in models, basically it represents your systems logical view.. As an example in my previous walkthrough, in my system I had Post class , Comment class
Lets dig more deeper.
Even though when we are represent our system with classes, their relationships etc. practically these classes cant store data (Ex : in our Blog project we said Blog has Posts, (its our logical representation.. ) )
So when practically implementing there should be a way to save our Posts, Basically we need a backend. Ideally a database.
In our good old days, when we want to save something in the database we had to do following steps, we had to write code
- Open the database connection
- Execute an SQL query to insert details
ORM and ActiveRecords
But things has been changed by now, now we have something called ORM (Object Relational Mapping), what does that means ?!!! it means your objects and classes in OO design will automatically match with your relational database. Simply each table maps to a class and row of your table represents an object of the class and each column represents an attribute of that object, the Big Big advantage of this is, you don’t need to worry about SQL (most of the time) , you simply create the object and ask it to save, it will do the rest..
Now, whats the rails support for ORM. Actually rails has a great support for ORM, rails does it through something called ‘ActiveRecord’ (its rails ORM mapper…) , you can inherit from the “ActiveRecord::Base” class and make your model class ORM enabled
By default rails assumes followings for ORM enabled model
- Model has a table which name is pluralize name if the model
Ex: if your model is ‘User’ your table should be ‘users’
- Your table has a auto increment id column which is your tables primary key
- If you have relationships between tables , foreign keys should <singular table name>_id
Ex: if Post has many Comments , comments table has to have a column called post_id
Note : Even though these are default settings you can change them as you want..
Remember this command ?
ruby script/generate scaffold Comment post_id:integer comment_description:text
what we do here is it creates a model called Comment which has two properties (post_id and comment_description), then this maps to a table called comments and it has 3 columns
id (remember is one of our golden rules..)
post _id
comment_description
And it inherits from ‘ActiveRecord’ , like this
class Comment < ActiveRecord::Base
Not only that, rails has some cool validations also, one would be
validates_presence_of
Syntax : validates_presence_of : comment_description
This means, when you try to update or create a comment rails will first check whether its description is filled, or else will give an error message (… as developers.. we simply call it and it will handle the rest..)
ORM and object relationships
Rails has an elegant way of defining relationships via models.
Ex : our posts has many comments, so inside the Post model you can do something like this
class Post < ActiveRecord::Base
has_many :comments
end
OK, that’s the overview of models. I think you have an Idea of what a model does. Then we have view. View is simply the presentation layer , it uses to take inputs and display outputs. Rails supports following extensions mainly
rhtml
rrb
html.erb
views will basically contains your html codes, css, javascripts etc..
and then the controllers
basic idea is , controllers are the communicators between the views and models, so the whole picture is something like this

Extracted from - http://www.akelos.org/docs/tutorials/booklink
Ok, Back to our framework, All these (models, views, controllers) are saved inside App folder in your rails directory (Figure 1.0)
Then there is another very important directory called Config. This will/ should have all the configurable settings of the system, (Ex : your database settings, etc..)
NOTE : if you change any one of these files , you should re-start your web server to make them effect.
Inside this you have some very important files (All are important, but I’m talking about the configurable files..)
databse.yml – this is has the settings for database connection, following is an example file

Adapters are the ones which tells rails what is the database that will going to be used. Ex : mysql, postgres, sqlite3 (rails default)
Wait.. Aren’t you missing something, can you see your database settings are repeated, under development, test and production, why is that ?!
That’s because by default rails allow your project to run in 3 different modes
Development – this is for you do run when you are developing the system, Some advantages should be errors will be shown with details, in-detail logs
Test – when your product is handed over to QA for testing, they might run test cases against this database
Production – when your application is really going live, no error details will be shown etc..
environment.rb
This is again an important file where we can define some constant variables.
One example would be say, you want to your users to redirect to google, then you can have a variable like REDIRECT_URL = http://www.google.com in environment.rb file and use the variable in your code. Advantage is that , later if you want to redirect to yahoo, there is only one place to change.
Db
Db is the folder which keeps your database creation scripts (rails call them migration.) , when you do a scaffold , rails will automatically creates a migration file for you . (you can even create only migration files)
Ex : ruby script/generate migration <table name>
And if you look at them closer you will see, its not SQL, why is that, remember we discuss about database adapters. There I said that, rails can be used with different databases! , So you may already know with different databases sql syntaxes are also slightly different. Because of that having a raw sql script might not work. That’s way rails has its own way of keeping scripts.
When you run the script with
rake db:migrate
rails will automatically gets the database adapter (from your database.yml) file and execute the script accordingly
public
public folder will have all the files which can be accessible through web, ex : stylesheets, images, javascripts etc…
test
Test is another important folder, rails comes with a full test suit and in my next article we will write some tests and get an idea about what these tests really means
Phew.. I have covered some of the important folders in rails frame works and some are ignored. That doesn’t mean those are not important, but I will get back to them later..
Rails / ruby is open source
Open source means there is no magic, you have the code, you can see what is happening. If you can see what is happening , it has a great chance that people go there and change the code, make the code better. Remember we are not talking about 20 -30 developers, this is hundred thousand developers who spreaded all over the word.
Those smart people often comes with some useful extenuations to rails (like , authentication, list pagination, etc..) , in rails we call them plugins.
So as I said, there are hundreds of developers developing plugins for rails.. In my next lessons I will show you how to use some useful plugins.
Rails makes developer lives easy :D
Needless to say more …
See you next time..



Post new comment