Seeking complete example/plugin of simple usage of Lookup (a


I think the second most useful usage of the auto_complete is for editing
relations (associations).

I’ve searched for almost a week on all RoR resources but I didn’t found
any solution to this problem. Instead I’ve discovered that there are
many looking for this.

I think that this problem must be clearly solved and the clear how-to
should published on one-more RoR sites.

Because I trust the power of examples I’ll try to explain the problem
using an example.

The description of the problem

  1. We have two models: Book(s) and Contact(s). A book has_one :contact.
    Contact doesn’t know about book.
  2. Because we have a lots of Contacts we can’t use dropdown for
    selecting a contact for a book.
  3. Also because contacts can have the same name we must submit the ID of
    the contact.
  4. Are interested in a magic way of mapping the relations on submit. We
    don’t want to write more code for every new editable relation.
  5. Additional optional behavior: require a valid lookup value (empty not

Ideas for implementation

The form should submit something like this: :book => { :name => “my
book”, :author => { :id => 123, :name => “John”} }

The new (improved) update_attributes should work recursively: when he
will find the :author key in the has it will know that :author is a
relation and will call update attributes for it.

I looking for comments, hints or ever portions of code related to this

PS. How do I make the Contact auto_complete to work in the book form
without inserting other instance variable?