Prepared statements in Rails

Rails 3.1 and above has an awesome feature of prepared_statements. The basic idea behind prepared statements is to compile SQL statements once and cached for it future use. In other words - The benefit to prepared statements is that the database does not have to compile a query plan for every piece of SQL sent to it, potentially saving a lot of time. Checkout an excellent blog(blog) to know more on prepared_statements. Recently, I had to get raw sql from AR object so that I can pass it to EXPLAIN statement to get total number of

Fragment caching and sweeping strategy

Rails provides three types of caching techniques: page caching action caching fragment caching The rails_guides_on_caching is the best place to know how it works. This article is to demonstrate fragment caching the Rails Way with various data-stores and clearing those cached fragments on ActiveRecord callbacks on CRUD operations. Rails ActiveSupport::Cache::Store is responsible to do all the read and write operations. 1. The setup By default caching is disabled in rails. You will find the following config commented out in production.rb # Use a different cache store in production # config.cache_store = :mem_cache_store You

Use syntax_fix gem to change old ruby hash style

Change old Ruby 1.8 hash syntax to new one in Rails app. syntax_fix gem changes the hash style from ':key => "value"' to 'key: "value"' Put this line in your Gemfile: gem 'syntax_fix' syntax_fix This command will check and rewrite all files with the new syntax. options: v <-- be verbose about the actions p [PATH] <-- specify the relative path to start actions from Reference: https://github.com/HeeL/syntax_fix

Things which Rails could do but I didn't know.

Run Helper methods in the console $rails c => helper.number_to_currency(100) => "$100.00" => helper.time_ago_in_words(3.days.ago) => "3 days" Shorthand Migrations $ rails g resource person first_name:string last_name:string email:string token:string You can write above command as- $ rails g resource person first_name last_name email token Both will generate same migration: class CreatePeople Add Indexes to migrations $rails g resource person first_name:index last_name email:uniq token class CreatePeople true end end Add Associations to migrations $ rails g resource company person:references name description:text We can

ActiveRecord::Dirty

Performing an action via a callback is fairly easy and doable. Sometimes requirement may be a bit enhance that we need to track every change now and then. On the same note, if we have associations and we wanted to make some changes that needs to be reflected on previous and new values, this dirty concepts can save our time. This module gives a way to track changes done in an active-record object. By default, rails include ActiveRecord::Dirty this module. This gives many powerful methods that helps us to track attribute changes. This methods are based on the attribute

Crawling and Scraping Techniques in Ruby

Problem case to solve : Crawl any given site(url), scrape each page uniquely for content from given selector or Xpath Available options for Crawling and Scraping : With [libxml2][1] [Nokogiri][2] is one of the fastest HTML/XML parser. Almost all of the scraping tools written in Ruby such as Mechanize, Wombat, Anemone, etc. uses Nokogiri as there base DSL for sraping. [Mechanize][3] - Is a beast in this category. It efficiently uses Nokogiri for HTML scrapping. Do see Abi's post on [Automating browser navigation's through Script][4] [Wombat][5] takes the scraping term to a next higher level,

Rails Store - A solution to store multiple value in single column.

What is ActiveRecord::Store? Store provides a simple means of accessing and storing key/value pairs related to a model. Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column. In simple words, it is used for storing mutiple columns under a single column. Its serailized/deserialized to save and load the data for easy access. Example You have a Music application, you want to cache the last volume adjustment for the user. So that when he returns back, same volume level is applied, similarly other volume related settings. Now you can

Faster Specs with FixtureBuilder

What is [fixture_builder][1] ? Fixture Builder creates fixtures based on your definitions of [FactoryGirl][2] Why fixture_builder ? In projects with huge test suite, its possible that the creation of AR objects hinders the test performance. So to gain performance, we can avoid the redundant database transaction by building subsequent fixtures. Examples https://github.com/rdy/fixture_builder#example It gives tremendous boost to your test execution time. For us the statistics for 550 test cases were : Before fixture_builder ~ 2.5 hours After fixture_builder ~ 19.37 minutes Note : This should be backed up by proper [DatabaseCleaner][3]

Backing up Databases and Directories

Backup your Database OR any of your personal stuff like directories with [backup][1] Backup's Components Archives and Databases Compressors and Encryptors Storages Syncers Notifiers Its a handy backup tool with plenty of storage options (for example: S3, Dropbox, FTP, SCP and many more) For installation(Dependencies should be installed if you are working with shell environment) gem install backup backup dependencies --install net-ssh backup dependencies --install net-scp backup dependencies --install excon backup dependencies --install fog Now execute the @backup generate@ command backup generate:model -t backup [-c ] --archives \ --storages='local,s3' --compressors='gzip' --notifiers='mail,twitter' This will create