A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Wednesday, August 21, 2013

Adding extensions to Postgresql database with Rails

The default Postgresql installation on Mac OS X using homebrew includes a single extension - plpgsql (PL/pgSQL procedural language) but there are a number of them available in the lib directory (/usr/local/Cellar/postgresql/9.2.1/lib on my machine)

To install an extension into the database, the easiest way I found was to open a database console using the 'rails db' command and then create it directly. I have seen mention of doing this in a Rails migration but this did not work for me.

Note that an extension is installed in a specific database, rather than being added to all databases.

The command to list the installed extensions is '\dx'

$ rails db
psql (9.2.1)
Type "help" for help.

tabs_development=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

tabs_development=# create extension unaccent;
CREATE EXTENSION
tabs_development=# \dx
                         List of installed extensions
   Name   | Version |   Schema   |                 Description
----------+---------+------------+---------------------------------------------
 plpgsql  | 1.0     | pg_catalog | PL/pgSQL procedural language
 unaccent | 1.0     | public     | text search dictionary that removes accents
(2 rows)

Friday, August 16, 2013

Testing for the presence of a loaded Gem in a Rails application

I'm migrating the search function in an application from meta_search to ransack (both from Ernie Miller)

The syntax has changed quite a bit between the two gems and I use the search functionality in more than 30 controllers / index pages. So I want a simple way to update each instance while still being able to switch back to meta_search if I need to.

So I need to test which of the two gems has been loaded in the running application.

The best way that I have found is this:

if Gem.loaded_specs['ransack']

'... do this...'

else

'... do that ...'

end

You can see the full list of loaded gems with Gem.loaded_specs.keys


Wednesday, August 7, 2013

Default Sort Order in Ransack Rails Gem

Ernie Miller's Ransack gem is a great way to handle searching and sorting of data in a Rails 3 application. It is the successor to his meta_search gem. There is a railscast episode on ransack.

But while Ransack offers benefits over meta_search, it is lacking in documentation which is a shame.

The problem I ran into was how to set a default sort order for records in the controller, before one had been set in the view.

Here is how you specify this:
      @search = Post.search(params[:q])

      @search.sorts = 'name asc' if @search.sorts.empty?

      @posts = @search.result.paginate(:page => params[:page],
                                 :per_page => 20)


The other problem that I am still dealing with is that Ransack does not appear to let you use scopes from your model in the search form. meta_search did allow this. That turns out to be a big issue in a current project to port an older application into Rails 3.

Archive of Tips