Sometimes in an app you need to do something in SQL and not in your app’s language. At t-bot we’re strictly MySql. And occasionally, MySql specific functions/syntax creeps into an app.
hasmany :publishedposts, :conditions => 'published_on < now()'
From an app that allowed users to future date their posts.
@post = Post.find :first, :conditions => ['year(createdon) = ? and ' + 'month(createdon) = ? and ' + 'day(created_on) = ? and ' + 'keyword = ?'] params[:year], params[:month], params[:day], params[:keyword]]
From an app that needed ‘pretty’ urls e.g. ‘posts/2007/1/15/some-blog-post’
Here’s a beauty doing some timezone logic in SQL instead of Ruby. This was done for performance reasons.
Event.find :all, :select => 'events.*', :joins => 'inner join localities on localities.id = events.localityid ' + 'inner join timezones on timezones.id = localities.timezoneid', :conditions => ['localityid = ? and ' + 'month(timestampadd(HOUR, timezones.utcoffset, startdate)) = ? and ' + 'year(timestampadd(HOUR, timezones.utcoffset, start_date)) = ?', locality.id, locality.timezone.now.month, locality.timezone.now.year]
timestampadd. That one scared me a bit.
Something even crazier from an app that need to ‘weigh’ comments depending on when they were made. Also done in SQL for performance reasons. This is from an
:select => "revisions.*, SUM(IF(comments.createdon > DATE(NOW())-INTERVAL 1 DAY, 5, IF(comments.createdon > DATE(NOW())-INTERVAL 1 WEEK, 3, IF(comments.c\ reatedon > DATE(NOW())-INTERVAL 1 MONTH, 1, 0)))) as commentscore",
I always hesitate when I have to dip into MySql because I don’t want to develop a dependency on it. Why? Because years of using generic database independent interfaces and an insistance on the ability to change databases without changing a line of code. The fact is I’ve never had to change a database vendor on an app ever. And to me that argument gets weaker and weaker every day.
If the time comes that you need to switch to PostgreSQL then tell your client that you’re going to need some time to account for any dependencies your app has developed on its old database. However, I doubt this is ever going to happen.
So stop feeling guilty about using