Ruby
ActiveRecord object caching in Ruby on Rails
Submitted by steven on Sat, 02/20/2010 - 17:03Yesterday I started playing with basic caching for PriceChirp and tried what I thought would be easy. Boy was I wrong. It turns out what I was attempting to do is not supported by :memory_store in the development environment. Before moving to :mem_cache_store, I was able to find a work around. The work around is outlined below for those who do not have the option of using memcached. However, if you can use memcached, it is by far the better route to take.
My goal was to cut down on the number of database hit by caching the resultant ActiveRecord object in :memory_store.
Number to Currency Outside Rails
Submitted by steven on Fri, 09/18/2009 - 07:02I created a module I am also using both in rails and for some batch processing outside of rails. In order to ensure my data was formatted the same with both environments, I needed to figure out how to get access to the active_view helper number_to_currency outside of rails. Here is how I did it:
require 'rubygems' require 'action_view' def number_to_currency(number, options = {}) ActionView::Base.new.number_to_currency(number, options) end
Rescuing from open-uri timeouts
Submitted by steven on Sat, 09/12/2009 - 22:54Found an issue with open-uri because today Amazon's API is wonky. The same issue will occur when accessing any remote data via open-uri (like a RSS feed) if the data source is going to slow. The relevant part of my ruby code looks like:
begin doc = Nokogiri(open(api_url)) rescue print "Connection failed: #{$!}\n" next end
For some reason, the rescue does not catch a timeout error.
/usr/lib64/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error)
Explicitly catching the timeout error fixes the code.
begin doc = Nokogiri(open(api_url)) rescue Timeout::Error print "Timeout::Error: #{$!}\n" next rescue print "Connection failed: #{$!}\n" next end
Good to know, as you never know when your remote data provider will be slow.
Generating Sitemaps in Rails
Submitted by steven on Fri, 03/27/2009 - 00:18Code updated 9/18/2009
This week I added sitemap files to my rails site PriceChirp. Sitemaps are used to help search engines to find all your content. They are especially helpful in enumerating pages that are difficult for web crawlers to discover, such as content from database searches.
The web is full of instructions on how to generate sitemaps on the fly using rxml templates. This does not scale well if your site has thousands of links. A better method is to periodically generate site maps and serve these cached files when requested.
www.fortytwo.gr has a good example for generating sitemaps with rails. I've taken his code and fixed/extended it to fit my needs.
Understanding Sitemaps
Basically, there are two types of sitemap files: