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.