Normally in rails, you can only render views inside of the controller. But what if you want to render a view somewhere else? For instance we wanted to generate xml-files using views. Haml can be used to describe xml just as well as plain html.

There is a gem called render_anywhere that allows just that. So how does this work, for example:

class Organisation < ActiveRecord::Base

  has_many :members

  include RenderAnywhere

  def to_xml
    render partial: "#{self.to_partial_path}", object: self, layout: 'my_xml_layout'

We had a little problem when using partials though.

Normally if you type something like

= render @member

it will ask the partial path from the model (@member.to_partial_path), but somehow this always got prefixed with render_anywhere. The gem creates a dummy RenderingController in the RenderAnywhere namespace, so apparently it will look for the following view:


In our case, I did not want to use the render_anywhere subfolder. It took me a while to figure out how to overrule this, but in essence it is pretty simple: rails uses the namespace of the rendering controller to prefix the path. Some deep googling proved that any controller has a method called _prefixes which lists all the prefixes for that class.

We can easily verify this in the rails console:

:001 > RenderAnywhere::RenderingController._prefixes
=> ["render_anywhere/rendering"]

So if we could overrule _prefixes to just return ["rendering"] … Mmmmmm fork the code of render_anywhere? Or …

There is another option: render_anywhere allows you to supply your own RenderingController and will use that instead if found in the context where the RenderAnywhere code is included.

So, if you write something like:

class Organisation < ActiveRecord::Base

  has_many :members

  include RenderAnywhere

  class RenderingController < RenderAnywhere::RenderingController

    def self._prefixes


  def to_xml
    render partial: "#{self.to_partial_path}", object: self, layout: 'my_xml_layout'

it will look for a view called members/member. Woot. To specify a different sub-folder you can adapt the _prefixes method as you wish :)

Developing rails websites with a geographic component we rely heavily on Postgis, so we use activerecord-postgis-adapter for the Postgis support, and I always use schema_plus because it allows me to define views. Until recently, I always had to use the structure.sql instead of the schema.rb because the geometric columns did not dump correctly.

But for a while now, activerecord-postgis-adapter handles this correctly and so we use the schema.rb file again. Only to discover a “new” error:

ActiveRecord::StatementInvalid: PG::DependentObjectsStillExist: ERROR:  cannot drop view geography_columns because extension postgis requires it
HINT:  You can drop extension postgis instead.
: DROP VIEW IF EXISTS "geography_columns"

Apparently specific Postgis views are also dumped in the schema file, and those views obviously cannot simply be re-created.

A very naive solution I kept using was to comment those create_view lines in our schema.rb file. But apparently there is a much better solution: you can configure which tables and views schema_plus should ignore.

So I added an initializer in /initializers/schema_dumper.rb with the following content:

ActiveRecord::SchemaDumper.ignore_tables = [
   "geography_columns", "geometry_columns", "spatial_ref_sys", "raster_columns", "raster_overviews"

And now my schema.rb is correct, and simple commands as rake db:setup or rake db:test:prepare just work. Hehe.

I have a very weird problem with my geoserver+oracle, when deployed on a Windows 2012R2 server (see here), and in attempting to solve that, I upgraded the geoserver from 2.6.3 to 2.7.1, hoping that that would fix that.

Sometimes fairy tales come true, but in this case it did not help, unfortunately. The 2.7.1 did render a lot quicker, except one layer which did not render at all anymore.

My style could not render with the error The requested Style can not be used with this layer. The style specifies an attribute of <missing attribute name>. Checking the layer in geoserver, I could see it was no longer to determine any of the attributes for the given table.

Further investigation in the logfile revealed the following (cryptic) error:

Failure occurred while looking up the primary key with finder: org.geotools.jdbc.HeuristicPrimaryKeyFinder@24cf7139

java.sql.SQLException: Exhausted Resultset

Mmmmmm. Luckily my google-fu revealed a linked issue, and simple solution:

update the driver from ojdbc14.jar to the newer ojdbc7.jar fixes this problem.

Hehe :)

Updating geoserver did not fix my problem: my layer still had some duplicate columnnames. This might not be such a big problem: everything is drawn correctly, WMS calls work, but WFS calls gave the irritating yet predictable error ORA-00918: column ambiguously defined. Annoying.

So how does one find column-names for a table in oracle? With a query like:

select * from dba_tab_columns where table_name = 'YOUR_TABLE_NAME';

and all of a sudden I saw the same set of column-names, with some duplication. Apparently my oracle database contains the table twice, in two different schema’s. Since my user had the permissions to access the other schema, it seems geoserver does not limit the query to the (specified) schema at all.

The fix then was easy: make the other schema unaccessible. In my case the second schema was for testing purposes, so I could just delete it.

At my current job, we make GIS websites, using rails and geoserver. I develop on mac, and for some clients we need to deploy on windows. One client is still using an Oracle database, while in general I prefer to work with postgis databases, and also geoserver offers better support when using postgis.

So: when working locally I got a really weird phenomenon in my geoserver: it duplicated various oracle columns. Generally, for viewing not a problem but when using WFS I got the “column ambigously defined”, and using Oracle SQL Views did not work (it went looking for meta data?) and the Geoserver SQL Views were painfully slow.

But on my clients server I installed Geoserver 2.6.3 and the oracle stuff just worked. Woot :) So I had to upgrade my ancient 2.3.3 geoserver. It is running inside a tomcat. Upgrading seemed easy enough: copy the old geoserver folder somewhere (actually you would only need the data folder and the web.xml but I am lazy/extra safe like that), and drop the new war, and theoretically, we should be good to go.

Except … I got this peculiar error in my log-file

SEVERE: Error listenerStart

WTF! Thanks to some googling, I added a file to my geoserver\WEB-INF\classes with the following content:

org.apache.catalina.core.ContainerBase.[Catalina].level = DEBUG
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

restarted my tomcat, and the following appeared:

SEVERE: Error configuring application listener of class org.geoserver.platform.GeoServerHttpSessionListenerProxy
java.lang.UnsupportedClassVersionError: org/geoserver/platform/GeoServerHttpSessionListenerProxy : Unsupported major.minor version 51.0 (unable to load class org.geoserver.platform.GeoServerHttpSessionListenerProxy)

Now what the hell cryptic error is that? But apparently this is a very compact way to say this code needs java 1.7 and I am still using java 1.6 (I am looking at you Apple). Updating now :)

The simplest way to select all distinct values of a column is, somewhat unintuitively:


this runs the query select distinct project from visits, and returns an array of strings. Exactly what you need, except … I want it to be paginated. So we


… and that completely bombs: we now get an array of numbers?

So we try something else, and write:'distinct project') 

which runs the good query, but returns an array of Visit‘s with only the project filled in. I can live with that. And then pagination (using the kaminari gem) is again as expected:'distinct project').page(params[:page])

Nice :)


I have a single bootstrap modal, which is called from different places, and so the modal contains some data-* attributes I want to set before showing it. Just using the .data() offered by jquery does not work.

Detailed example

Suppose you have mark-up like this:

<div data-some-important-value="123">

Asking the value is quite easy:


And, according to the documentation, setting the data on a DOM element, should be as easy as

$('[data-some-important-value]').data('some-important-value', 'new-value')

If you would try this in the console, you could verify that does not work. This is where it gets confusing (to me). Apparently the .data() offered by jquery existed before the HTML5 data-* elements did, and they nicely integrated them. But the data-* are only loaded once, and never written back to the document.

To still be able to do this, use the .attr() method instead:

$('[data-some-important-value]').attr('data-some-important-value', 'new-value')

Now I only have to include one modal “template” in my HTML, and set the data-* attributes to customize the behaviour.

I started out writing something like the following:

If I could change just one thing in ruby, I would change how private works. Aaarrgghh.

For instance, I am trying to implement an equality of bounding boxes in ruby. I would presume I could write something like:

class BoundingBox

  attr_accessor :xmin, :ymin, :xmax, :ymax

  def initialize(xmin, ymin, xmax, ymax)
    @xmin = xmin
    @ymin = ymin
    @xmax = xmax
    @ymax = ymax

  def ==(other)
    other.class == self.class && self.state == other.state
  alias_method :eql?, :==


  def state
    [@xmin, @ymin, @xmax, @ymax]


Because I am inside the scope of the class, I am allowed to access the private methods. That is how it is supposed to be. But no, ruby says something silly like:

private methods cannot be called with an explicit receiver

So you have to resort to something ludicrous like writing

  def ==(other)
    other.class == self.class && self.send(:state) == other.send(:state)

This works. So I could have left it there. But wait: protected is exactly what I want. I have been using it wrong completely.

So in short:

  • use public for your public API of your class (obviously)
  • use protected for methods that are not part of your API, but other instances of the same class need to be able to reach (e.g. for equality checking, sorting, …)
  • use private for methods that are only used inside the class, and only refer to the implicit receiver.

So if in the first example I change private to protected, it just works. I have been doing it wrong all along.

Most of the times I use a config.yml to store application settings, that I want to be able to change quickly between environments, servers, deployments. But what with settings that need to be changed on the fly, by a user?

I create a small model, with three fields.

rails g model Setting name:string description:string value:string

I use a seed file to define the different settings. Settings are checked in code, but I can not preload them, since a user might change them.
So first I added a method as_hash that loads all settings, and I can then directly use a hash, but that gets wordy quickly.

What if … I could provide a method on the class Setting for each setting in the database? That would be really nice. This seems like a job for … method-missing-man Special superpower: seeing places where method_missing could be used :)

class Setting < ActiveRecord::Base

  validates_presence_of :name

  def self.as_hash
    settings = {}
    Setting.all.each do |setting|
      settings[] = setting.value

  # offer all settings as methods on the class
  def self.method_missing(meth, *args, &block) #:nodoc:
    @all_settings ||= Setting.as_hash
    if @all_settings.keys.include?(meth)

For documentation, the test:

context "convenience: define methods for all settings" do
  before do
    Setting.instance_variable_set('@all_settings', nil)
    Setting.create(name: 'my_other_test_setting', value: '123ZZZ')
  it "Setting.my_other_test_setting returns the correct result" do
    Setting.my_other_test_setting.should == '123ZZZ'
  it "an unexisting setting behaves as a normal missing method" do
    expect {
    }.to raise_exception(NoMethodError)

I love ruby :) :)


If you are using brew to manage your postgresql/postgis install, and you suddenly cannot access any postgis functionality, with the error that cannot be found, check your json-c version, I had to do

brew switch json-c 0.10

to get it working.

The long and dirty story

I had created a new database, but for some reason I could not add a geometry column, so I was thinking, maybe, somehow my POSTGIS extension needs to be (re)activated. And when I tried this I got the the obscure error

could not load library "/usr/local/Cellar/postgresql/9.2.3/lib/": dlopen(/usr/local/Cellar/postgresql/9.2.3/lib/, 10): Library not loaded: /usr/local/opt/sqlite/lib/libsqlite3.0.8.6.dylib   Referenced from: /usr/local/lib/libgdal.1.dylib


Maybe my postgis installation is somehow corrupt, so I tried brew install postgis. Wronggggg move. Suddenly I am installing postgresql 9.3.4 too?

Ok. I did not see an alternative. This of course ment I should upgrade my database. First step: install postgresql 9.3.4 and postgis. Then I tried to follow this upgrade procedure. In short I issued the following commands:

initdb /usr/local/var/postgres9.3 -E utf8
pg_upgrade -d /usr/local/var/postgres -D /usr/local/var/postgres9.3 -b /usr/local/Cellar/postgresql/9.2.3/bin/ -B /usr/local/Cellar/postgresql/9.3.4/bin/ -v

and that failed? I got almost the same error, but now a bit more verbose:

PG::UndefinedFile: ERROR:  could not load library "/usr/local/Cellar/postgresql/9.2.3/lib/":   
    dlopen(/usr/local/Cellar/postgresql/9.2.4/lib/, 10): Symbol not found: _json_tokener_errors
Referenced from: /usr/local/Cellar/postgresql/9.2.4/lib/
Expected in: /usr/local/lib/libjson.0.dylib
   in /usr/local/Cellar/postgresql/9.2.4/lib/

What? Json? And that led me to the answer: I had to switch my json-c version:

brew switch json-c 0.10

Restarted my pg_upgrade which now seemed to work, but failed at the end, that and were not loadable. Sigh. These were of course compiled against the new json-c (I think?).

I switched the json-c version back to 0.11 and then I started my postgres process again. This showed my that my databases were NOT upgraded.

This almost feels like dll hell al over again.

Should I uninstall postgis, do db_upgrade and install it again? Go back to 9.2.3?

For the moment I switched back to 9.2.3 and opened an issue on the homebrew. I hope somebody can help me.

 brew switch postgresql 9.2.3
 brew switch json-c 0.10

Not sure what will break, because something needed to install json-c 0.11 ? At least for now I am good. I hope.

[UPDATE] Nope. It only partly works now. The errors I saw were:

  • after brew switch json-c 0.10 :

    PG::UndefinedFile: ERROR: could not load library “/usr/local/Cellar/postgresql/9.2.3/lib/”: dlopen(/usr/local/Cellar/postgresql/9.2.3/lib/, 10): Library not loaded: /usr/local/lib/libjson-c.2.dylib
    Referenced from: /usr/local/opt/liblwgeom/lib/liblwgeom-2.1.1.dylib
    Reason: image not found

  • after brew switch json-c 0.11

    PG::UndefinedFile: ERROR: could not load library “/usr/local/Cellar/postgresql/9.2.3/lib/”: dlopen(/usr/local/Cellar/postgresql/9.2.3/lib/, 10): Symbol not found: _json_tokener_errors
    Referenced from: /usr/local/Cellar/postgresql/9.2.3/lib/
    Expected in: /usr/local/lib/libjson.0.dylib
    in /usr/local/Cellar/postgresql/9.2.3/lib/

So I was stuck. Reverting to the old version did not fix it.

I was able to get my situation rectified by doing a “clean” install of postgresql92 which installed postgresql 9.2.8.

brew install postgresql92
brew link --overwrite postgresql92
brew install postgis20
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

And now I am good. On version 9.2.8. Hehe.


I have no clear conclusion or solution. Somehow my json-c got upgraded, which messed up my postgis installation. I guess installing the new version of postgis, messed up my old version of postgis (since now one file was linked to json-c.0.11 and the other against json-c.0.10).

However I did find a clean solution: upgrading to postgresql92 and postgis20, now nicely linked against json-c 0.11.

If you encounter the same error: switch your json-c version, before trying anything else, and then I hope you should be good to go (no need to upgrade).