don't use :conditions with auto_complete_for
Posted by admin, Mon Feb 13 08:14:15 UTC 2006
Despite what the documentation might lead you to believe…
By default, auto_complete_for limits the results to 10 entries, and sorts by the given field.
auto_complete_for takes a third parameter, an options hash to the find method used to search for the records:
do not attempt to look up the “options” hash from the find method and expect all the options to work as advertised. Especially do not use the :conditions option. If you do, your auto-complete will be silently but completely broken.
We discovered this in a pair-programming session this morning. Over the past week, my partner and I had individually attempted to customize a particular auto-complete_for in order to limit the returned values to a specific subset. To give you a simple “fer-instance”, lets talk about 2 tables, people and houses. Houses belong to people. But not all people have houses. We needed an auto-complete on people that would limit the values returned to people who own houses.
Once again, the ability to look at the source (thanks RDoc) came to the rescue. We eventually figured out that using :conditions would override the auto_complete_for :conditions. So we just created our own customized auto_complete_for method for this case.
If anyone has a better solution, I’d be happy to learn about it.