Search This Blog

Wednesday, August 29, 2012

ROR - things i think u must know!

database.yml
Although rails follows 'convention over configuration' you need to configure things like database before working on a project. database.yml is the database configuration file for any Ruby on Rails project. It is present under the config directory of your project root folder.
So what is yml ? its a file extension of YAML files.
YAML --> YAML ain't markup language. Yes , it is a recursive acronym :)
However when it was being developed YAML meant yet another markup language. It was then changed to current name to stress the importance of data rather than markup.
In nutshell its just a human readable data serialization format. NOTE: it uses strict spacing. I missed a space and managed to fix the error only after an hour !

RESTful way!
In rails we do applications that are RESTful.
A resource is generally associated with entities which can be created,read,updated,deleted ( CRUD operations ).
I will explain what is RESTful with an example,
suppose you have a website (www.vehicles.com) that deals about different vehicles.
Now,
www.vehicles.com/lorry/wheels/2
This is RESTful. It clearly means you are accessing the 2nd wheel of a lorry.
the same can be implemented as,
www.vehicles.com/getvehicleaslorry/parts/getpart?id=2
It looks horrible and it is certainly not RESTful.

In Rails RESTful way comes naturally. You tend to have a controller for a resource and actions like create,destroy follow.

Models and ActiveRecords.
Every model inherits from ActiveRecord::Base.
Understand that ActiveRecord is a module. Its like a namespace. Base is a class inside it.
The ActiveRecord::Base class gives methods to access the records/fields of the table corresponding to that model using objects of that model itself.
For example, we have a users table which has the field name.
Now in the model User.rb,
user=User.new(:name=>"raj") 
will create an object user.
user.save
will save the object as a record in the users table.
Instead of two separate statements you can do
User.create(:name => "raj")
It will save create a record in user table having name "raj"



No comments:

Post a Comment