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,

result.class
=> ActiveRecord::Relation

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

Comments

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'

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

PayUMoney Integration in Ruby on Rails

PayUMoney Integration in Ruby on Rails


Generating Cheksum in Payu
   Hash or Checksum
= sha512(key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||salt)



Controller Code Will be
def payumoney
  @key = "gtKFFx"
  @salt =  "eCwWELxi"
  val = "#{key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||#{@salt}"
 @checksum = Digest::SHA512.hexdigest("#{val}")
end

<form action="https://test.payu.in/_payment" method='post' id='sub'  >
    <input type="hidden" name="key" value="<%=@key%>">
    <input type="hidden" name="hash" value="<%=@checksum%>">
    <input type="hidden" name="txnid" value="TXN1234">
    <input type="hidden" name="amount" value="200">
    <input type="hidden" name="firstname" value=&quo…