Include a second table in auto_complete_for

Hi,

I have 2 tables such as:

  create_table "professionals" do |t|     t.integer "user_id"     t.string "telephone"   end

  create_table "users" do |t|     t.string "nicename"   end

...and I would like to edit the auto_complete_for line to include the professionals table, in order to only find nicename of professionals user.

class ProfessionalsController < ApplicationController   auto_complete_for :user, :nicename, :limit => 15, :order => 'nicename DESC'   (...) end

Is that possible to joint the professionals table by using :conditions for example ?

Thanks for any help.

You can write your own auto completion method:

   def auto_complete_for_user_nicename       @users = ...       render :inline => '<%= auto_complete_result @users, :nicename %>'    end

Xavier Noria wrote:

Is that possible to joint the professionals table by using :conditions for example ?

You can write your own auto completion method:

   def auto_complete_for_user_nicename       @users = ...       render :inline => '<%= auto_complete_result @users, :nicename %>'    end

I don't really see how to use this method. I also had trying to joint the professionals table with this:   auto_complete_for :user, :nicename, :limit => 15, :order => 'nicename, :include => :professionals but that do not work because I just can add a :conditions option.

The auto_complete_for macro just generates a method with that name.

You need to *remove* the macro and *implement* the method. Your custom action auto_complete_for_user_nicename typically takes what the user has typed from the params hash and issues a custom query. If you trace Ajax calls with firebug you'll see auto completion just invokes that action (by default). In the action you are free to do anything to figure out the completions and send the results back to the browser.

Once you have the collection in @users the auto_complete_result helper eases building the results the way the client expects them.

Xavier Noria wrote:

The auto_complete_for macro just generates a method with that name.

You need to *remove* the macro and *implement* the method. Your custom action auto_complete_for_user_nicename typically takes what the user has typed from the params hash and issues a custom query. If you trace Ajax calls with firebug you'll see auto completion just invokes that action (by default). In the action you are free to do anything to figure out the completions and send the results back to the browser.

Once you have the collection in @users the auto_complete_result helper eases building the results the way the client expects them.

Thanks a lot. Now it's okay :wink: If that could be useful, I copy past my method:

  def auto_complete_for_user_nicename     @users = User.find(:all, :include => :professional, :conditions => ['user_id > ? and nicename LIKE ?', 0, "%#{params[:user][:nicename]}%"])     render :inline => '<%= auto_complete_result @users, :nicename %>'   end