Auto completion with composite field

How can auto completion be done for a composite field ?
Concrete problem : Suppose the class Person with reference to a City
object.
The City has a name and a postal code.

When entering a Person I want to enter the city in one text field that
shows a composition of the city name and the postal :
“city (postal)”. And this with auto complete (and should afterwards be
linked to the person object)

I don’t think that textfield_with_auto_complete solves the problem. any
ideas ? I read about auto_complete_field, but I didn’t find any concrete
sample code… I couldn’t figure out what to do in the partial, the
controller, the actual form

On 7/14/06, Dieter D’haeyere [email protected] wrote:

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Perhaps composed_of may help here.

in you model use the composed_of method to specify it

composed_of :city_pcode, :mapping => [ %w(address_city city),
%w(address_pcode pcode)]

Then in your controller use
auto_complete_for :city_pcode

I haven’t tried this so it may not work. If it doesn’t you could use a
custom set and get method.

def city_pcode
“#{city} #{pcode}”
end

def city_pcode=( city_and_pcode )
self.city, self.pcode = city_and_pcode.split( " " )
end

Then again in your controller use
auto_complete_for :city_pcode

At least I think that should work.

Cheers