Gabe Hollombe
2005-Oct-22 18:41 UTC
Using auto_complete_for with an attribute that doesn''t map directly to a column in the db
Hi all, I''ve been trying to figure out if I can use auto_complete_for with an attribute in a model that doesn''t map to a db column name directly, and I''ve had no luck. It just doesn''t seem to work. For example, I have first_name and last_name columns in my person table, but i want to do auto_complete_for :person :full_name, (where :full_name = :first_name + :last_name. Is this possible to do with the simple vanilla auto_complete_for helper? Thanks in advance, - Gabe Heres my code, for reference... in controllers/records_controller.rb: auto_complete_for :person, :full_name in views/records/new.rhtml: Select Person: <%= text_field_with_auto_complete :person, :full_name %> in models/person.rb: def full_name "#{@first_name} #{@last_name}" end
Julian ''Julik'' Tarkhanov
2005-Oct-22 22:38 UTC
Re: Using auto_complete_for with an attribute that doesn''t map directly to a column in the db
On 22-okt-2005, at 20:41, Gabe Hollombe wrote:> Hi all, > > I''ve been trying to figure out if I can use auto_complete_for with > an attribute in a model that doesn''t map to a db column name > directly, and I''ve had no luck. It just doesn''t seem to work. > > For example, I have first_name and last_name columns in my person > table, but i want to do auto_complete_for :person :full_name, > (where :full_name = :first_name + :last_name. Is this possible to > do with the simple vanilla auto_complete_for helper?Most likely not, because autocomplete operates on the database level (it just uses a LIKE query to do attribute lookup). You can write your own method which will involve adding concatenation to this query (look in the documentation). -- Julian "Julik" Tarkhanov
Gabe Hollombe
2005-Oct-22 22:47 UTC
Re: Using auto_complete_for with an attribute that doesn''t map directly to a column in the db
Many thanks, Julian. =) - Gabe On Oct 22, 2005, at 6:38 PM, Julian ''Julik'' Tarkhanov wrote:> > On 22-okt-2005, at 20:41, Gabe Hollombe wrote: > > >> Hi all, >> >> I''ve been trying to figure out if I can use auto_complete_for with >> an attribute in a model that doesn''t map to a db column name >> directly, and I''ve had no luck. It just doesn''t seem to work. >> >> For example, I have first_name and last_name columns in my person >> table, but i want to do auto_complete_for :person :full_name, >> (where :full_name = :first_name + :last_name. Is this possible to >> do with the simple vanilla auto_complete_for helper? >> > > Most likely not, because autocomplete operates on the database > level (it just uses a LIKE query to do attribute lookup). > > You can write your own method which will involve adding > concatenation to this query (look in the documentation). > > > -- > Julian "Julik" Tarkhanov > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Forrest Thiessen
2005-Oct-23 04:08 UTC
Re: Using auto_complete_for with an attribute that doesn''t map directly to a column in the db
Gabe Hollombe <gabe@...> writes:> > Hi all, > > I''ve been trying to figure out if I can use auto_complete_for with an > attribute in a model that doesn''t map to a db column name directly, > and I''ve had no luck. It just doesn''t seem to work. > > For example, I have first_name and last_name columns in my person > table, but i want to do auto_complete_for :person :full_name, > (where :full_name = :first_name + :last_name. Is this possible to do > with the simple vanilla auto_complete_for helper?I was just working on the same thing. Yes, you can easily do this with Rails. No, you can''t do it with auto_complete_for. Using auto_complete_for causes Rails to dynamically generate a "typical" AJAX action to respond to requests made by the browser code generated by the text_field_with_auto_complete helper. In this case, "typical" isn''t good enough, so you simply write your own action, instead--it''s not hard. Your action just has to have the right name (auto_complete_for_person_full_name) and must render a partial (which you also write) that returns an HTML list of the items you want displayed, formatted as you want them to appear. Amy Hoy wrote a great tutorial on this subject: http://www.slash7.com/articles/2005/08/13/ ajaxariffic-autocomplete-with-scriptaculous Here''s a quick stab at what it might look like in your case: # In controllers/records_controller.rb: def auto_complete_for_person_full_name @people = (a query based on params[:person][:full_name], which contains what''s been typed so far) render :partial => ''full_name_auto'' # Name can be whatever you want end # In views/records/_full_name_auto.rhtml <ul class="people"> <% for person in @people do -%> <li class="person"><%= person.first_name -%> <%= person.last_name -%></li> <% end -%> </ul> --Forrest
Gabe Hollombe
2005-Oct-23 14:05 UTC
Re: Re: Using auto_complete_for with an attribute that doesn''t map directly to a column in the db
Thanks, Forrest. I''d found Amy''s tutorial, and was ultimately going to go the way of writing my own action anyway. I was just wondering if there was a way I could get the vanilla auto_complete_for to check a non-db attribute in my model. Looks like I''ll need to do it the way you mention, and looks like it won''t be hard at all. =) - Gabe On Oct 23, 2005, at 12:08 AM, Forrest Thiessen wrote:> Gabe Hollombe <gabe@...> writes: > > >> >> Hi all, >> >> I''ve been trying to figure out if I can use auto_complete_for with an >> attribute in a model that doesn''t map to a db column name directly, >> and I''ve had no luck. It just doesn''t seem to work. >> >> For example, I have first_name and last_name columns in my person >> table, but i want to do auto_complete_for :person :full_name, >> (where :full_name = :first_name + :last_name. Is this possible to do >> with the simple vanilla auto_complete_for helper? >> > > > I was just working on the same thing. > > Yes, you can easily do this with Rails. No, you can''t do it with > auto_complete_for. Using auto_complete_for causes Rails to > dynamically generate > a "typical" AJAX action to respond to requests made by the browser > code > generated by the text_field_with_auto_complete helper. In this > case, "typical" > isn''t good enough, so you simply write your own action, instead-- > it''s not hard. > Your action just has to have the right name > (auto_complete_for_person_full_name) > and must render a partial (which you also write) that returns an > HTML list of > the items you want displayed, formatted as you want them to appear. > > Amy Hoy wrote a great tutorial on this subject: > > http://www.slash7.com/articles/2005/08/13/ > ajaxariffic-autocomplete-with-scriptaculous > > Here''s a quick stab at what it might look like in your case: > > # In controllers/records_controller.rb: > def auto_complete_for_person_full_name > @people = (a query based on params[:person][:full_name], which > contains what''s been typed so far) > render :partial => ''full_name_auto'' # Name can be whatever you want > end > > # In views/records/_full_name_auto.rhtml > <ul class="people"> > <% for person in @people do -%> > <li class="person"><%= person.first_name -%> <%= person.last_name > -%></li> > <% end -%> > </ul> > > --Forrest > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >