Category: Ruby

  • Organise photos by date

    I organise my photos by grouping them in a directory, where the directory takes on the name “created_date title” (e.g. 2016-05-16 Birthday Party). As it became tedious to create a directory and manually group photo sets, I wrote a Ruby script to assist with the heavy lifting. After that, it’s just a matter of providing…

  • Resetting password of Devise user from the database

    I recently encountered a situation where I was trying to access a Rails application but: I didn’t know the password for the admin user The reset password functionality was not working I had no access to a Rails console Knowing the User model was using Devise, I set a password for a user on a…

  • Get database version in Rails

    For one of my applications, I recently created a status report page which shows crucial information for debugging, including software versions, service connectivity and filesystem path writability. One of the items was a database version string, and the standard ActiveRecord interface does not support this concept. While version methods usually exist in the connection adapters,…

  • Optimising index view performance

    I recently came across a scenario where my index views in Rails were taking a long time to render when there were large data sets. The problem stemmed having a lot of data presented that were from models/tables other than the primary model/table of interest. For example, an “asset” index view may have the following…

  • Selecting string literals in Rails 4 using PostgreSQL

    I recently ran into an issue with Rails 4 using the PostgreSQL adapter (MRI) where selecting a string literal in a query would return a warning about an unknown OID. This can be reproduced by running the following in Rails Console: This occurs both with and without column aliases. 705 is PostgreSQL’s code for an…

  • Nokogiri XML schema validation with multiple schema files

    When using Nokogiri to validate an XML document against multiple XML Schema files using import declarations, ensure that you use File.open rather than File.read as shown in the Nokogiri::XML::Schema documentation. This will allow Nokogiri to navigate to and read these imported schemas. Thanks to this StackOverflow post. a.xsd b.xsd example.xml validate.rb

  • Selecting by Option Value using Capybara

    Capybara’s select command allows searching a HTML select field and selecting an option that matches the supplied value by name, id or label text. Additionally, the match option allows the user to indicate the behaviour if more than one option is found. For example, match: :first will select the first item out of multiple matches,…

  • Using Rails’ descendants in development

    Rails provides a nice method descendants that returns all subclasses for a specified class. However, config.cache_classes = false is a default setting in development.rb (for the ability to reload classes on the fly) and as this tells Rails not to load classes until they are referenced, then calling descendants will generally return an empty array…

  • Moving to a Windows JRuby on Rails development environment

    tl;dr I recently set up a JRuby on Rails development environment on Windows after using Mac OS X and Ubuntu VMs. It’s noticeably faster for development and more comfortable. I’ll stick with it. When I was first starting JRuby on Rails development, I started with a Mac OS X environment running from VirtualBox. I chose…

  • Reducing Rails asset precompile times on JRuby

    Rails asset precompile times on JRuby are considerably slower compared to MRI. I came across this post which provided suggestions on speeding up the asset precompile task. Using the following options – using Node.js instead of therubyrhino for JS compilation, forcing the JVM to 32 bit (although this can be omitted on a 32 bit…