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(@tutors+@courses, :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 ? -- Posted via http://www.ruby-forum.com/.
The trouble free method would be to create a "view table" using both tables and use it to do auto complete. -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 18:41 UTC
Re: Auto_Complete plugin with results from two tables
Rails List 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 ? -- Posted via http://www.ruby-forum.com/.
Hello Benoror, I think "railslist" meant a virtual table in you data base usually call view (http://en.wikipedia.org/wiki/View_%28database%29). 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 Sousa -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 21:04 UTC
Re: Auto_Complete plugin with results from two tables
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, :namesupposing I''m implementing my own method, with no success I guess the problem might be at:> render :inline => "<%= auto_complete_result(@tutors+@courses, :name) %>"Becaus the find''s are working properly. What can it be ? -- Posted via http://www.ruby-forum.com/.
> > I guess the problem might be at: >> render :inline => "<%= auto_complete_result(@tutors+@courses, :name) %>" > > Becaus the find''s are working properly. >Have you try to merge then before sending?>> @tutors+=@courses >> 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 Sousa -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 22:35 UTC
Re: Auto_Complete plugin with results from two tables
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 :(> 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 :( -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 22:37 UTC
Re: Auto_Complete plugin with results from two tables
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. -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 22:57 UTC
Re: Auto_Complete plugin with results from two tables
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-- Posted via http://www.ruby-forum.com/.
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 Sousa -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 23:06 UTC
Re: Auto_Complete plugin with results from two tables
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 ? -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 23:10 UTC
Re: Auto_Complete plugin with results from two tables
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 -- Posted via http://www.ruby-forum.com/.
Benoror Benoror
2009-Jul-21 23:27 UTC
Re: Auto_Complete plugin with results from two tables
FIXED! The problem was stupid. The method was ''protected'' Inheritance ignorance! Thanks David! -- Posted via http://www.ruby-forum.com/.