Tuesday 30 July 2013

Switching between Mongoid and ActiveRecord

I have recently been working on 2 projects, one using Mongoid with MongoDB and one using ActiveRecord with Postgres. Working so closely with the two pretty much alongside each other I have come to the following conclusions:

  • MongoDB should not be used for relational systems. It might seem obvious, but even if the min body of an application is not relational, there are often relational parts to it, and that can really counteract the benefits. Such relations might include user ownership, tagging, etc.
  • I like the declaration of fields within the model - no need to make external schemas.
  • Having said the above, I also like the discipline that creating migrations forces on you. You can really check each change you make to models and refine them, whereas the flexibility of Mongoid can allow indiscipline.
I think that, for the moment, in most projects I would still stick to a RDBMS unless there was an obvious document-style structure to the whole data that could really benefit from MongoDB.

Monday 29 July 2013

default_url_options[:host] fails with http://

This is something that gets me every single time. If you set up your code as follows (in the config/environments/development.rb file:

config.action_mailer.default_url_options =  {host: 'http://localhost:3000'}

then any links you have in there will not work correctly as they will be pointing to http://http://localhost:3000, which will break email clients. Instead you have to do it without the protocol and it all works fine:

config.action_mailer.default_url_options =  {host: 'localhost:3000'}

Wednesday 17 July 2013

IE7 and IE8 text rendering problem

We recently changed the design of an internal site I was working on for BMW and suddenly the text started rendering less clearly in IE7 and IE8. We were using a webfont, but that had not changed at all between the designs. After much investigation I realised that we had introduced a gradient behind the text. This was implemented in IE using:

filter: progid:DXImageTransform.Microsoft.gradient

Turning these gradients off fixed the rendering problem. We therefore reverted back to using images for IE:

background-image: url( 'background_image.png' );

So, it appears it is not a good idea to place text using a webfont over gradients created using the gradient filter in Internet Explorer.

Tuesday 16 July 2013

Hash default array

Trying to do something in Ruby on Rails I just encountered the following problem:

I just had this:

hash = Hash.new([])
for...
    hash[key] << value
end

to make the default value a new array. I have since realised that this does not work as it uses the same array for each key in the hash. Meaning that every key returns the same array with all the values.

The way I worked round it was as follows:

hash = {}
for...
    arr = hash[group._id] || []
    arr << value
    hash[group._id] = arr
end

Friday 12 July 2013

Rspec route testing with Mongoid

This may be obvious to others, but I have just been having a lot of frustration trying to test routes with a rails application written using Mongoid.

The problem was that the id of the object being created is not a string. The id therefore needs to converted to a string to make it work.