Ruby On Rails Interview Questions and Answers
Ques 21. What deployment tools do you use for Ruby on Rails?
Capistrano is a popular deployment tool.
Ques 22. What deployment tools do you use for Ruby on Rails?
Capistrano is a popular deployment tool.
Ques 23. How can you migrate your database schema one level down?
The rake tool does most of the migrations.
It has this nifty syntax to go back one step:
rake db:rollback
If you want to rollback all the way to the beginning you would use:
rake db:reset
This would drop the database, recreate the Database and load the current schema into it
If you want to rollback multiple steps at the same time you would use:
rake db:rollback STEP=3
To rollback all the way and if you are not worried about losing the data then you can drop the database completely with purge like this:
rake db:purge
Ques 24. What is Sweeper in Ruby on Rails?
Sometimes you want to have control over how often and when the cache expires.
Sometimes it is a good idea to have the system determine that on a logical basis. Say you have a list of product on your site and you want to reload the cache each time a new product is added/updated/deleted, then you can achieve this by using the sweeper.
class ProductSweeper < ActionController::Caching::Sweeper
OBSERVE PRODUCT# This sweeper is going to keep an eye on the Product model
# If our sweeper detects that a Product was created call this
def after_create(product)
expire_cache_for(product)
end
# If our sweeper detects that a Product was updated call this
def after_update(product)
expire_cache_for(product)
end
# If our sweeper detects that a Product was deleted call this
def after_destroy(product)
expire_cache_for(product)
end
private
def expire_cache_for(product)
# Expire the index page now that we added a new product
expire_page(:controller => 'products', :action => 'index')
# Expire a fragment
expire_fragment('all_available_products')
end
end
Ques 25. How can you implement Caching in Ruby on Rails?
Rails offers multiple ways to cache content.
Fragment caching is my favorite because it gives you the choice to fragment to pull a portion from the cache and the remaining from a real-time DB call.
Say you wanted to show all the orders placed on your website in real time and didn’t want to cache that part of the page, but did want to cache the part of the page which lists all products available, you could use this piece of code:
<% Order.find_recent.each do |o| %>
<%= o.buyer.name %> bought <%= o.product.name %>
<% end %>
<% CACHE DO %> All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
Another technique that works well for static pages is page caching. This technique is often used for home pages and is super fast.
class ProductsController < ActionController
CACHES_PAGE:index
def index
@products = Products.all
end
end
Most helpful rated by users:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?