Skip to main content

Lazy loading in rails – Rails Feature

 Lazy loading in rails – Rails Feature ?

Lazy loading in rails is the amazing feature provided with rails. In console you might have tried to examine how lazy loading in rails actually works. In this tutorial, we will learn about this Rails - Lazy loading feature with examples.
What exactly is Lazy Loading?

As the name suggests the data is loaded in lazy manner (Really!)

i.e. Your database is queried only when data from the database is required for some kind of manipulation in code. You will get more of this after you read how-to of lazy loading below.
How lazy loading works:

Whenever you try to get some data from database,

For example, users is the database table that you have. And you are querying database to get users having age less than 20.

Then, you will write code like,

result = User.where("age < 20")

when above statement is executed, your database is not queries yet(because the resultant data is not required yet).

When you execute following code,

records = result.all

That, you asked for data (All rows from the required data). This is when actually query happens on database and it returns all rows of the result set are returned to results variable. So, results will be array of ActiveRecord's. This is what lazy loading in rails is!

When database is actually queried (end of lazy loading)?

The database is queried when data is required or asked for, in cases like all, first, count, each. These will trigger database load and thus lazy loading will end.
Rails Console and Lazy loading in Rails

This is where you need to be patient. We will take same example as discussed above for examining lazy loading on rails console. We can start rails console by command

rails console or rails c

Getting data from users table having age less than 20. Then you execute command,

result = User.where("age < 20")

And hit enter. Then you will see that console has printed array of ActiveRecord's as result. And thus you might think that query has already been expected on your database and lazy loading hasn't worked quite well. But, this is not what happened. You got this result because rails console inspects last statement that it executes which gives the array of ActiveRecord's.

i.e. If you try

result = User.where("age < 20"); nil
=> nil

Then you will see that rails console has printed nil and not array of ActiveRecord's as a result. As the last statement that is executed was nil.

There is simple way to check whether lazy loading is in place (working correctly) -

Just check the class of result by,

=> ActiveRecord::Relation

It is ActiveRecord::Relation which means database is actually not queried. And it is just the way console works.


Popular posts from this blog

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?

1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below


class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

<link rel="canonical" href="<%= request.url %>" />
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address