Monday, 27 August 2012

More on Rails!!

Rest:Representational State transfer.This is associated with resources.
What is a resource??Anything that is an entity and also allows and implemetnts Crud and index method.
What is a model??  Anything that interacts with a database is a model.In rails you need not create your database,nor establish access from the scratch.All this is automatically done by the ORM-object relation mapper.

What does an ORM do??It's job is to connect to the database.The attributes are mapped to the columns of the database and the classes are mapped towards the tables in databases.The objects of the classes are mapped to the records of the database.In the tables used in rails the foreign key and the primary key are not used to create the relationships.Instead it is abstracted and relationships are created by the means Associations in OOPS.

Some of the associations are-->has_one
-->has_many
-->has_and -belongs to many
-->belongs+to
In cases where you map 2 tables in a many-many relationship then you have to include an intermediate table to it.The intermediate tables will contain the id's of both the columns.We do not add a seperate id column to this because it is not actually necessary.When you make associations certain dynamic methods are created during the runtime,which is an instance of ruby programming.For ex if user and groups are associated then the methods such as "user.groups" and group.user are created.Assocaition in rails makes it very simpler to  use.

ActiveModel and ActiveRecord:
                         When you create a class,the model class inherits from the ActiveRecord.Active record allows you to access the db records,manipulate it and retrieve it and to perform the other logic necessary.It wraps a table inside an object.The mapping of the objects to rows,attributes to columns etc are done via this.When a new object is created a new row is creates.For instance
part=new Part
part.name="rr"
part.type="qq"
part.save
 will do
insert into part values("1","rr","qq");

Similarly it can also be used to query a database.

What is the ActiveModel then??!.It is the behaviour which allows the ruby objects to act like its active records,by adding validations,callbacks,naming conventions etc.
Activemodel doesnt relate to the tables in the database.

Consider a shopping cart table it can also be put directly in the payment table and paid status can be put to false,but it is a better practice to put it in a seperate table cos only then the dynamic methods like user.shop_cart can be accessed easily else we have to write queries checking for the required records.Also test classes are made simpler.

Callbacks in ActiveRecords:
This is an action performed directly after a DB action.Like a callback is called soon after an object is saved or before that.There are various such callbacks.After_initialize is another important callback.Callbacks are similar to triggers.To enable the proper working of callbacks Hooks are introduced.
To create A hook

create s_hook<ActiveRecord::Hook::Base
   Callbacks helps you in not  writing too much into the model and messing it up.Instead you can put it in the callbacks.When there are large number of callbacks then put it together into a observer.There are certain actions like "update_all"  which will not directly trigger the validations,must use such actions with great care.

Db:
schema.rb locks your db schema while executing.At each migration schema.rb is updated.
For Mongo db like db there is no Db.In such ORM's there is no migration associated with it,so no locking of schemas.

You can either inherit from Active_Record or include its modules.Which is better??When you inherit you are including a lot  of unwanted code.So it is better you include and use the necessary modules as necessary.

Now we will move on to actionpacks which contains Actionview and actionController.
ActionController is thr first to get hit when an request is sent.
What is applicationController??It contains the general settings,like
-->protect_from_forgery for the entire application
-->If signed in go to one page or the other page.
All other controllers are inherited from the application  controller.

Now we know what the application controller does ,so what does a action controller do>>
-->It has ability to call and use the actions
-->Enables code abstraction and so methods can be called as actions.

certain actions in a controller can also be made as a private action .But how will you cal them??Yes it can be called via another method.ie some other action can call this action.
But where exactly do we use private methods in a controller???I am yet to find the answer to this question,will come back with an answer very soon..

Yeahh and so there are many instances where you use private methods in a  controller.Such as if there are subsequent actions that should be done in some other database tables when some db action occurs it can be done in  private methods in a controller.But be sure that the subsequent db action you do is accessed only by self.

If you want to change from the actual form to some other form ,the flow is iven in your controller.You give it as
   render :action=>status
where status is the new view file name.

Render and Redirect_to:
     Render is only used to redirect to some other form.But when you give redirect you go to some other view,but the control goes to the controller and the model before rendreing this view.

Partials:
   Bunch of HTML code for reuse.
   Start s with an "_".

Do you find any instance in rails where you can easily see the block concept implemented.Yes in the layouts the html files basic layout will be given and in the body part the "yield" will be called and this part is given in the various views files.


No comments:

Post a Comment