time_diff - a gem for calculating the difference between two TIME

Yesterday I created a gem for calculating the difference between two TIME.

When I was working in my office, we found that there is no method to extract the difference between two Time in a useful format like years, month, week, day, hour, minute and second.

time1 - time2 return only difference inseconds.

So I created this gem time_diff which return the hash with difference:

{ :year => 0, :month => 0, :day => 0, :week => 0, :hour => 0, :minute => 0, :second => 0 }


State Machine

In some application we need to track the state of a model. For example, a user requested for signup. Admin has to approve the user for the user to login. Admin has the option to reject the user also. So the status of the user model will be in one of the following states [pending, approved, rejected].

The famous plugin used for tracking the state is aasm. You can install Acts as state machine using:

sudo gem install aasm

Then in the user model you have to include the plugin

include AASM

Then specify which field you want to use as the state.

aasm_column :status

Now you can write the states of the model:

aasm_state :pending 
aasm_state :approved 
aasm_state :rejected

You can set initial state using:

aasm_initial_state :pending

Now we need the events to change the state:

aasm_event :approve do
  transitions :to => :approved, :from => [:pending]

aasm_event :reject do
  transitions :to => :rejected, :from => [:pending]

This is all we need in model.

Now to change the state of the model object you can use:

user.approve!  #To approve the user
user.reject!      #To reject the user

If you want to do something while the state of the model is change from one state to another you can call the method at the time of state change using:

aasm :approved, :enter => entered_to_apporoved, :exit => exit_from_approved

At the time of state definition you can specify the enter method and exit method to call.



Heroku is a PAAS (Platform As A Service) for Ruby on Rails. You will get a free domain with limited support for your Rails App. After signup to Heroku, you can install the gem:

gem install heroku

Then you can start your application with create command:

heroku create yourapp

Using git commands you can deploy this app to the Heroku:

git add, git commit,

git push heroku master

This will deploy to your heroku repository, git @heroku.com:yourapp.git
And your URL will be, http://yourapp.heroku.com

To do the db:migrate run the command:

heroku rake db:migrate

For more details visit Heroku



If you have a web page with a list of data more than a screen height, it won't be look good. The user have to scroll down to see the data, and mostly it will affect the performance of your website. If there are 100 rows of data in a list, we will fetch all the data to show in the list. But if we are using pagination we can limit the number of data to fetch, for example if we need to show only 10 rows of data per page we only need to fetch 10 records, not the entire data.

There is a gem called will_paginate which do all we need to do for a pagination in our rails web app. All you need to do is install the gem:

gem install will_paginate

Then when you are accessing model for data, use the paginate along with that:

@data = Data.paginate :page => params[:page]

Here params[:page] is the page number we need to fetch. This will come from the view because we will add some helper method in the view to show the page links:

<%= will_paginate @data %>

This code you can put it in the end of the list, so that when you click on the link it will send request to the same action with parameter as next page number.


through association

There are many associations in Rails, like one-to-one, one-to-many, many-to-many. You can associate a model to another using any of these associations. So what is 'through' association?

Take an example of three models: teacher, student, subject

There is no direct association between teacher and student. So using through you can create association between teacher and student models.