Nathan Wilmes's blog



Interesting Things

  • One of our clients had a large production issue due to a long-standing bug in Rails with case sensitivity.

Here's the situation: Rails validates_uniqueness_of has a flag called :case_sensitive. This flag defaults to 'true', but can be flipped.

MySQL's default collation is case-insensitive. As a result, queries will, in general, ignore case unless specifically overridden.

So one might imagine that setting :case_sensitive to false would be completely harmless in a standard MySQL application.

One would be wrong. Setting case_sensitive to false changes the query to lowercase the field in question, causing the MySQL database to ignore any indices it may have and turning the validates_uniqueness_of operation from something cheap and quick to something requiring a full table scan.

The open Lighthouse ticket on this issue is: https://rails.lighthouseapp.com/projects/8994/tickets/2503-validates_uniqueness_of-is-horribly-inefficient-in-mysql

Nathan WilmesNathan Wilmes
Standup 5/20/2010: More about SEO routes
edit Posted by Nathan Wilmes on Thursday May 20, 2010 at 09:13AM

Ask for Help

"Any clever ways to catch out of bounds exceptions from Solr?"

This is a follow-up to yesterday's Solr question. After some investigation, it looks like none of the major providers catch out of bound exceptions for very large numbers. Rather than instrumenting every Ruby call with validations to prevent these numbers from getting into Solr, are there any other brilliant ideas?

Interesting Things

  • Follow-up to the help from 5/19/2010's SEO routing question. The latest hotness appears to be FriendlyId (http://github.com/norman/friendly_id) This plugin makes human-friendly slugs and comes with a variety of interesting features, including versioning and slug scoping.

  • Power RubyMine commands:

Goto File + line #: If you use ctrl-shift-N to go to a file, try typing in a line number after a colon, something like "my_file:30". You'll end up on that line.

Analyze stack trace: This tool lets you paste in an external stack trace, and gives you the ability to browse to all of the pieces of that stack trace.

Nathan WilmesNathan Wilmes
Standups 5/18/2010 and 5/19/2010: SEO routes
edit Posted by Nathan Wilmes on Wednesday May 19, 2010 at 09:19AM

Ask for Help

"Our site requires crafting URLs in a very particular SEO-friendly way. Rails doesn't seem to give us a good solution for our URLs. Any suggestions?"

One of our clients needs to make their app accept and generate compound URLs that look something like the following:

http://my.site/jkrowling-series-harrypotter-book-1

where author, series, and book are all different domain concepts. Rails RESTful resources don't really support this format. There wasn't an immediate solution, but among the peanut gallery of ideas:

  • Hyphens are better than slashes in URL crafting, but Rails doesn't separate on slashes at all

  • to_param solutions - Overriding to_param to something that starts with an integer ID generates URLs that look very slug-like, but can use standard Rails Domain.find mechanisms. For example, a book.to_param might be overridden to be "1-bookname", which works for all purposes. The problem with this solution is that it doesn't quite fit the requirements here, and doesn't cover the compound needs.

  • Custom routes are always a possibility. You can hook up a special (non-resource) controller that understands flexible browse-y routes like the one above, parses them, and delegates to the more standard resource controllers. The problem here is that you have to figure out a decent delegate pattern and route generation pattern.

  • In general, URL crafting is a separate art from domain model crafting, and Rails doesn't really cater to this. You will have to design URL-centric code to suit your URL crafting.

"Any ideas on ways to performance test IE7?"

No immediate ideas, but potentially more later.

"When users enter very large search parameters for numbers we get the following exception out of RSolr:"

RSolr::RequestError: Solr Response: For_input_string_11111111111111111111__javalangNumberFormatException

Is there an elegant solution to this aside from validating that all input parameters aren't larger than max int?

Interesting Things

  • When using named scope methods that refer to other named scope methods, you may discover that your SQL has some redundant condition clauses. This is a bug in Rails 2, and has been true for several versions. However, it's a harmless bug - MySQL will understand the extraneous condition clauses just fine, without performance implications.

  • Mocking Paperclip for tests is a careful art. See our other blog post: Stubbing out Paperclip ImageMagick in Tests