# Include a second table in auto\_complete\_for

**URL:** <https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684>\
**Category:** Rails\
**Created:** [June 29, 2008, 11:22am UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684 "2008-06-29T11:22:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 29, 2008, 11:22am UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684/1 "2008-06-29T11:22:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 29, 2008, 11:39am UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684/2 "2008-06-29T11:39:17Z")

</div>

On Sun, Jun 29, 2008 at 11:22 AM, David B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 29, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684/3 "2008-06-29T12:26:39Z")

</div>

Xavier N. 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.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 29, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684/4 "2008-06-29T12:49:32Z")

</div>

On Sun, Jun 29, 2008 at 12:26 PM, David B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 30, 2008, 11:51am UTC](https://www.ruby-forum.com/t/include-a-second-table-in-auto-complete-for/141684/5 "2008-06-30T11:51:52Z")

</div>

Xavier N. 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 😉  
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
