Pagination

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.

Read More...

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.


Read More...

Cron Job

Cron is a time based job scheduler. Cron job is a job scheduled to run at particular time. In rails you may need to create cron jobs in many situations. For example send account expiration mail, check account balance and inform the user, etc. There is a gem 'Whenever', which allows you to create a cron job.

Whenever

Using whenever you can easily create cron jobs. First you have to install the gem:
gem install whenever

Then from your application path type:
whenevrize .
This will create the file 'config/schedule.rb'.

In scedule.rb file you can write your cron job:
every 1.day, :at => '12:00 AM' do
   runner "Model.method"
end 
this will run the method inside the Model everyday at 12:00 AM.


For more details visit GitHub

Read More...

Delayed Job

Delayed Job is a plugin used to execute long running jobs in background. For example, downloads, uploads, external interactions etc. And you can also specify when to start a delayed job.

To install delayed_job plugin:

script/plugin install git://github.com/tobi/delayed_job.git

After installation you need to create a table for the delayed job to save the jobs.

script/generate migration create_table_for_delayed_job

And add the following script to the migration file.

class CreateTableForDelayedJob < ActiveRecord::Migration
    def self.up
       create_table :delayed_jobs, :force => true do |t|
          t.integer :priority, :default => 0
          t.integer :attempts, :default => 0
          t.text :handler
          t.text :last_error
          t.datetime :run_at
          t.datetime :locked_at
          t.datetime :failed_at
          t.string :locked_by
          t.timestamps
       end
    end

    def self.down
       drop_table :delayed_jobs
    end
end



Now run the migration:

rake db:migrate


To add a job to delayed_job you can use the enqueue method of Delayed Job. First you have to create a Job Class, for example we need to send a notification mail using delayed job. Then we create a lib file with the content:

Class DelayedNotificationMail
   attr_accessor :id


   def initialize(id)
      self.id = id
   end


   def perform
     UserMailer.deliver_notification_mail (User.find(id))
   end
end

Now we can add job to the delayed_job using:

Delayed::Job.enqueue DelayedNotificationMail.new(@user_id), 0, 1.hour.from_now


To run the jobs in background you have to execute the rake task:


rake jobs:work


This will start a process which will run the jobs in the queue, and will run the job according to the delay specified in the job.

To clear all the jobs in queue you can use the rake task:

rake jobs:clear

For more details you can go to the GitHub

Read More...

dependent

'dependent' is an option used with association in rails.

Association
An association is the relationship between models. There are associations like one-one, one-many, many-many. To represent association in rails, it uses has_many, belongs_to, has_one and has_and_belongs_to_many.

For Eg: Account model contains many Members. It is represented as:

In Account Model:
has_many :members

In Member Model:
belongs_to :account

dependent
In the above example if we delete one record from the accounts table it will affect the members table. The members table will contain the invalid account_id. In these situations we can use 'dependent'.

In Account Model:
has_many :members, :dependent => :destroy

It will automatically deletes all the dependent data from the models.

Read More...