Rails gem rails3-jquery-autocomplete: How do I query multiple fields

后端 未结 6 1599
借酒劲吻你
借酒劲吻你 2020-12-03 11:18

I\'m using the rails3-jquery-autocomplete gem found here: http://github.com/crowdint/rails3-jquery-autocomplete

The instructions are clear for how to qu

6条回答
  •  时光取名叫无心
    2020-12-03 11:44

    An alternative to the previous suggestions is to define a SQL view on the table holding first_name and last_name. Your SQL code (in SQLite) might look like:

    CREATE VIEW Contacts AS
    SELECT user.id, ( user.first_name || ' ' || users.last_name ) AS fullname
    FROM persons;
    

    Then define a model for the table:

    class Contact < ActiveRecord::Base
        set_table_name "contacts"
    end
    

    You can now use rails3-jquery-autocomplete 'out-of-the box'. So in your controller you would write:

    autocomplete :contact, :fullname
    

    In your view file you can simply write:

    f.autocomplete_field :contact, autocomplete_contact_fullname_path
    

    I will leave configuration of the route as an exercise for the reader :-).

提交回复
热议问题