Tag Archives: Rails

Kickstarting mongrel with 503 errors.

So, I’m a total sucker for punishment, and thus I run a very small rails (mongrel) app on my website (shared host). Every once in a while, the mongrel app dies for some reason (server was rebooted, power loss, etc), and the PID file isn’t removed. So I tried an experiment. I told apache to use a PHP file for 503 errors (Service Unavailable — or what my users see when apache tries to rewrite to a mongrel that isn’t there). Then I wrote setup the PHP file to try and start rails, and email me if it fails. Read below for all of the PHP code and shell files used. Continue reading

Random SQL query in Ruby on Rails

SQL doesn’t seem to have a good standard on the name for a random function. In my app, I wanted to select a record that had not been updated recently. So I went with a ORDER BY ‘last_cache DESC, RAND()’

This would be perfect if I was using MySQL… But I use SQLite for development on my local machine. SQLite wants the function to be called RANDOM(). To make matters worse, Rails doesn’t seem to have a database agnostic way to go about this.

So for the short term, I went with a little hack to get things working everywhere. In my config/development.rb I defined SQL_RANDOM_FUNCTION = ‘RANDOM()’ and I also defined it in my config/production.rb file as ‘RAND()’. Then in any query I need the random function, I just concat SQL_RANDOM_FUNCTION to the string and it all works.

It isn’t really ideal, but it isn’t like my DB will randomly change to PGSQL overnight, without me knowing.