My latest Rails project is live
I’ve been working on a project for a while. The site has been live for a month or so, and seems to be stable. If you visit my site to read the posts, you may have noticed the top section of the left sidebar has been showing a widget from my new site VideoSlush:http://videoslush.com
VideoSlush:http://videoslush.com is a trading game for YouTube videos. You find cool videos that you think may get a lot of views in a short amount of time. You ‘buy’ the video and also take a guess at how many views it may get by noon the next day to earn a bonus. Then you see how your video does over time, using the graphs to see when the price peaks and then selling.
Sign-up is free and open, so check it out.
Making Rails fragment caching usable
Caching is a real pain to get right. Actually, the caching part is quite easy with Rails, its the expiry that causes the trouble. All the simple cases are easy! Its when you have a page with various pieces of data, with different expiry conditions that the trouble starts.
I’ve written some code to use versioning in my fragment cache that is working for my most recent project. This technique relies on using Memcache to work correctly. I’ve been thinking about packaging this up as a plugin and releasing it. Luckily, I never got around to it.
Today, I read this post on how to do better caching. This plugin has documentation and tests, and is easier to configure than mine.
Go check it out.
http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/
Rails validates_uniqueness_of is completely broken
If you are using the Rails implementation of validates_uniqueness_of in your model to ensure duplicate data doesn’t get into your database, your application is broken. If you don’t use the database to ensure uniqueness with a key, then your app will fail at some point in the future. Probably just when you start getting some decent traffic and would like the app to not fail.
How do I know this? It happened to me this last weekend.
In checking this out, I find that Michael Koziarski of the Rails core team recently wrote
validates_uniqueness_of gives a nice error message, and does an ok job at guaranteeing uniqueness. Validates uniqueness of + a unique index does both.
In my app, does a nice job isn’t really good enough when it comes to uniqueness.
Michael has also posted that, hey, if you don’t like it, fix it and submit a patch, which is a great idea, but for this case is a really hard problem to solve for the general case across many databases.
I didn’t find a lot of specific fixes with code that I liked, so here’s how I fixed it in my app.
For my case, I already had a do some stuff and save the model method. In this method, I added some code (I stripped out my app specific code in the example).
DUPLICATE_ERROR_MESSAGES = [
"Duplicate entry"
]
def save_new
begin
save
rescue ActiveRecord::StatementInvalid => error
if DUPLICATE_ERROR_MESSAGES.any? { |msg| error.message =~ /#{Regexp.escape(msg)}/ }
logger.info "Duplicate Entry exception from DB"
errors.add_to_base('Duplicate item not allow')
return false
else
raise
end
end
endI also added a unique key to the table to ensure the MySQL database throws the exception that the above code relies on.
I still keep the validates_uniqueness_of call, as this does give a more specific error message, but now I don’t rely on it to enforce uniqueness.
Shh, don’t tell DHH that I’m putting integrity checks into my database.