Rails’ auto_complete integration is pretty nice for building search functionality, but for entry forms it all too often falls short.
Typically if I’m using an auto complete field in a form it’s because I want to link that record with an existing entry in another table, such as connecting an event with a location, or a book with a publisher, and that won’t work because the value obtained from the auto completion is the name of the linked entity, not its ID. I’ve tried various ways of managing that, either by overriding the standard methods or by adding some extra code in my model that will convert a name back into the associated model ID, but they all feel like hacks and I’d not had time to clean up the code and package anything as a plugin.
Thankfully Xavier Nora‘s already done it with the model_auto_completer plugin. The plugin provides a set of helper methods that will allow you to use auto completion, but have the ID of the record found stored as a hidden field in your form, making processing considerably easier.
Initially I had a problem using the plugin, and in fact found my rails application falling over because the plugin wasn’t getting the right name for the foreign key and since I have other fields that allow entry of a new location, rails couldn’t tell if a post with:
should use the first location data or the later data.
Looking through the code I found that instead of using:
foreign_key = real_object.class.reflect_on_association(association).options[:foreign_key]
to get the foreign key it should be using:
foreign_key = real_object.class.reflect_on_association(association).primary_key_name
Having submitted a patch, I was very pleased to hear over the weekend that the change has been integrated into the plugin so I can let piston update back to the official trunk. With that change in place, the plugin is a great way to make data entry much more convenient.