Auto_Complete plugin with results from two tables

Hi people, I’m trying to use the auto_complete plugin in rails 2.3.2 for
searching from two tables. I’ve researched a lot but I can’t make it
work. This is my code:

------- Application Controller: -------

class ApplicationController < ActionController::Base

auto_complete_for :tutor, :name

protected

def auto_complete_for_tutor_name
@tutors = Tutor.find(:all, :conditions => [“enabled = ‘t’ AND
LOWER(name) LIKE ?”, “%#{params[:tutor][:name]}%”], :limit => 10)
@courses = Course.find(:all, :conditions => [“enabled = ‘t’ AND
LOWER(name) LIKE ? OR LOWER(code) LIKE ?”, “%#{params[:tutor][:name]}%”,
“%#{params[:tutor][:name]}%”], :limit => 10)
render :inline => “<%= auto_complete_result(@[email protected], :name)
%>”
end

end

------- Form View: -------

<%= text_field_with_auto_complete :tutor, :name, {:autocomplete =>
“off”}, {:skip_style => true} %>


The point is that if I delete my “custom” auto_complete_for_tutor_name
method from the controller it works, for tutors names only of course.
What am I doing wrong ?

The trouble free method would be to create a “view table” using both
tables and use it to do auto complete.

Rails L. wrote:

The trouble free method would be to create a “view table” using both
tables and use it to do auto complete.

Should I create a new model/controller/view o can I create something
like a “virtual” table ?

Hello Benoror,

I think “railslist” meant a virtual table in you data base usually call
view (http://en.wikipedia.org/wiki/View_(database)).

But, if you are using Pat Shaughnessy’s plugin you may want to take a
look at
(
http://patshaughnessy.net/2009/4/3/filtering-auto_complete-pick-lists-part-2-using-named-scopes
). He explain how to use auto_complete_for passing a block and using a
name_scope query to the model.

By the way, what are you trying to do? Search for the course and tutor
name in the same input field?

best,

David S.

I guess the problem might be at:

render :inline => “<%= auto_complete_result(@[email protected], :name) %>”

Becaus the find’s are working properly.

Have you try to merge then before sending?

@[email protected]
render :inline => “<%= auto_complete_result(@tutors, :name) %>”

Does tutors and courses have the field name?

Just guessing here, I don’t know what it can be.

David S.

Thanks for your help David.

I have also tried writing my custom method just with tutors, but it
doesn’t works either!!! I guess the problem is related with something
else :frowning:

def auto_complete_for_tutor_name
@tutors = Tutor.find(:all, :conditions => [“enabled = ‘t’ AND LOWER(name) LIKE ?”, “%#{params[:tutor][:name]}%”], :limit => 10)
render :inline => “<%= auto_complete_result(@tutors, :name) %>”
end

:S No clue what can it be :frowning:

Is there a way to exactly know what does “auto_complete_for :tutor,
:name” generates ?

So I can rewrite it and jus make the appropiate modifications.

Yep, I want to search for course and tutor name in the same input box,
but I’m not using Shaughnessy’s plugin, I’m using the standard
auto_complete plugin.

I’ve tried commenting the line

#auto_complete_for :tutor, :name
supposing I’m implementing my own method, with no success

I guess the problem might be at:

render :inline => “<%= auto_complete_result(@[email protected], :name) %>”

Becaus the find’s are working properly.

What can it be ?

I’m out of ideas here, but you can see the code at the
vendor/plugins/auto_complete, or the on line version at:
http://github.com/rails/auto_complete/blob/7b18f05af68dec0f9e45b1a01d6b7c94e306caeb/lib/auto_complete.rb

With you are going to edit this, do not forget to restart your server
after saving. ( I think that function is called only once at the start
up. )

Good luck,

David S.

I found this code inside
vendor/plugins/auto_complete/lib/auto_complete.rb . Could this be used ?

module ClassMethods
def auto_complete_for(object, method, options = {})
define_method(“auto_complete_for_#{object}_#{method}”) do
find_options = {
:conditions => [ “LOWER(#{method}) LIKE ?”, ‘%’ + params[object[method].downcase + ‘%’ ],
:order => “#{method} ASC”,
:limit => 10 }.merge!(options)

   @items = object.to_s.camelize.constantize.find(:all, find_options)

   render :inline => "<%= auto_complete_result @items, '#{method}' %>"
 end

end
end

I can’t see anything wrong in my code. I’m starting to think that the
problem resides that the methos is defined in the ApplicationController.
Could it be ?

Benoror Benoror wrote:

I can’t see anything wrong in my code. I’m starting to think that the
problem resides that the methos is defined in the ApplicationController.
Could it be ?

That’s it!!!

I cant call my custom auto_complete_for… from ApplicationController !

But this is a feature I want along the entire Site, it’s unacceptable to
duplicate the code in EVERY CONTROLLER!!!

:S Thes must be a bug from the plugin, it can’t be called from
ApplicationController

FIXED!

The problem was stupid. The method was ‘protected’

Inheritance ignorance!

Thanks David!

Lol, I saw that was protected but I did not realize that.

The problem was stupid. The method was ‘protected’

David S.