SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Monday, September 15, 2008

Caching with Instance Variables

To improve performance &to store the end result of an expensive command in an instance variable!

code at first


# application.rb
def current_user
User.find(session[:user_id])
end


# application.rb
def current_user
@current_user ||= User.find(session[:user_id])
end

For the first one every time when the function is called it will create new instance to the database.

For the second one uses caching with or symbol which will only excutes the code when instance variable is not ready.next time it will returns the value

This article was taken from rails cast for my studying purpose