Posts Tagged Rails

  • Extending the timeout for Rails processes through Passenger on Nginx - August 5th, 2009

    I recently had to allow for a long running reporting task in a Rails app which is running under Passenger (mod_rails) on Nginx
    The default setting is 60 seconds and setting send_timeout to 300 in the Nginx config wasn’t working.
    After much searching, I found the answer. Edit
    /ext/nginx/Conguration.c and change the lines:
    ngx_conf_merge_msec_value(conf->upstream.send_timeout, prev->upstream.send_timeout, 60000);
    ngx_conf_merge_msec_value(conf->upstream.read_timeout, prev->upstream.read_timeout, 60000);
    to (for [...]

  • Rails plugin that provides rake tasks for identifying missing specs - June 18th, 2009

    I have a bad habbit of generating my views while specing the controller and then never generating a view spec.
    I wrote a rake task to tell me which views have no specs, kind of like a rcov for views.
    I then realised that it could be useful to find all files with missing specs so now [...]

  • How to select an array of values in Rails - June 15th, 2009

    I recently wanted to get a list of IDs from rails, but wanted them in an array.
    First try is to use :select => ‘id’ in:
    Model.find(:all, :select => ‘id’)
    but that creates a model for each id
    In order to get an array of values, you can use select_values:
    Model.connection.select_values(’select id from models’)
    This is great, but what if you [...]

  • Accessing the Rails logger inside of library files - June 10th, 2009

    A quick tip, if you’re developing a library which lives inside of RAILS_ROOT/lib, you can use the Rails logger via DEFAULT_RAILS_LOGGER
    class MyLib
      def my_method
        RAILS_DEFAULT_LOGGER.info "my log message" if RAILS_DEFAULT_LOGGER
      end
    end
    I added if RAILS_DEFAULT_LOGGER so that it doesn’t trip up if used outside of RAILS

  • Download a large amount of data in CSV from Rails - June 3rd, 2009

    I’ve just had to re-write an export script because the application now needed to be able to download 14000 records in CSV format.
    Originally I was reading all the records (with multiple joins) and then creating a csv string and then writing it to the response using send_data.
    The original code
    This is a simplified version of my [...]

  • Map Fields - A Rails plugin to ease the importing of CSV files - June 3rd, 2009

    I’ve just released a plugin, map-fieilds, that eases the importing of CSV files.
    When importing CSV files for a project, I wanted to add flexibility for the users so that they could import their CSV files in a looser format and then map their format to the format I needed.
    map-fieilds will intercept calls to a method [...]

  • Conditionally include development gems - May 28th, 2009

    I was playing with metric_fu and after having to install multiple dependency gems I realised that it was going to be a pain to put the project into production.
    The metric_fu instructions suggest using config.gem in your environment file but that will mean that when you push the app to production, you need to install the [...]

  • Setting the session base domain in Rails 2.3 - April 29th, 2009

    Prior to Rails 2.3, you could set the session base domain as follows:
    ActionController::Base.session_options[:session_domain] = ‘.example.com’
    In Rails 2.3, this needs to change to:
    config.action_controller.session[:domain] = ‘.example.com’
    I usually set this in my config/environments/.rb configuration files but it could be set in config/environment.rb if you want it to apply across configurations.
    Background
    This setting will allow for a shared session across [...]

  • New Rails plugin - Quick Scopes - January 27th, 2009

    I’ve just release a new Rails plugin called Quick Scopes that adds some generic named_scopes to your models to quickly manipulate the results you receive from associations.
    It’s great to have easy access to methods to get all associations but sometimes you need to manipulate how you receive those results.
    Included named_scopes:

    limit - to limit the number [...]

  • How to protect downloads but still have nginx serve the files - January 13th, 2009

    I’ve just been working on a project where a number of downloads needed to be restricted to specific users. I needed to authenticate the user and then allow them access to the file. This is not too difficult in rails:
    def download
      if authenticated?
        send_file #{RAILS_ROOT}/downloads/images/myfile.zip’
      end
    end
    The problem with this is that if the file [...]

←Older