Hello all, I''m in my third month of Ruby on Rails. I think it''s coming along fairly well. My current issue is this. I''m do a search based on string data some of which is very long. So I''m doing a similar match using "amatch" gem. When I have a 50% or greater match I store the id to an array. How can I do a find looping thru the array then send the query results to a partial. I have the following... - projects_controller - def search # get list of search parameters from id sent. list = params[:id] # split by comma list = list.split('','') # assign each id_search_string = list[0] title_search_string = list[1] # create new instance of Levenshtein using ''amatch'' m = Levenshtein.new(title_search_string) # retrieve all titles from projects @title = Project.find(:all, :select => ''DISTINCT id, title'') # create projects array projects = Array.new # loop thru all titles @title.each do |project| # if title search string more than 50% similar to the project title. if m.similar(project.title) >= 0.5 puts "ID:"+project.id.to_s + ", " + m.similar(project.title).to_s # push to array projects.push(project.id) end end # Next, we need to access all matching ids then query project table for ids. # loop thru array # HERE''S WHERE I''M LOST projects.each do |m| m.id end #QUERY USING SEARCHLOGIC, IF EASIER I COULD USE JUST "FIND" projects = Project.id_like(id).paginate(:page => params[:page], :per_page => 20) #RENDER PARTIAL render :partial => ''projects/results'', :locals => {:projects => projects} end Thank you for any help on this. JohnM -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Mcleod wrote:> Hello all, > I''m in my third month of Ruby on Rails. I think it''s coming along > fairly well. > My current issue is this. > > I''m do a search based on string data some of which is very long. So I''m > doing a similar match using "amatch" gem. > When I have a 50% or greater match I store the id to an array. > > How can I do a find looping thru the array then send the query results > to a partial.Use Array#find. Go read the Array class docs; there''s lots of good stuff there. Note that in most cases you should let the DB do the finding. Your solution is only appropriate here because DBs tend not to have Levenshtein distance functions. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --0022158df84f2b7535047c962537 Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0022158df84f2b7535047c962537--
On Jan 7, 11:03 am, John Mcleod <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello all, > I''m in my third month of Ruby on Rails. I think it''s coming along > fairly well. > My current issue is this. > > I''m do a search based on string data some of which is very long. So I''m > doing a similar match using "amatch" gem. > When I have a 50% or greater match I store the id to an array. > > How can I do a find looping thru the array then send the query results > to a partial. > > I have the following... > > - projects_controller - > > def search > # get list of search parameters from id sent. > list = params[:id] > # split by comma > list = list.split('','') > # assign each > id_search_string = list[0] > title_search_string = list[1] > > # create new instance of Levenshtein using ''amatch'' > m = Levenshtein.new(title_search_string) > # retrieve all titles from projects > @title = Project.find(:all, :select => ''DISTINCT id, title'') > # create projects array > projects = Array.new > # loop thru all titles > @title.each do |project| > # if title search string more than 50% similar to the project > title. > if m.similar(project.title) >= 0.5 > puts "ID:"+project.id.to_s + ", " + > m.similar(project.title).to_s > # push to array > projects.push(project.id) > end > end > # Next, we need to access all matching ids then query project table > for ids. > # loop thru array > > # HERE''S WHERE I''M LOSTYou''ve been pushing ids to the projects array, so all you should need to do is pass that to a Searchlogic scope like id_in, or the equivalent condition directly in the call to paginate. So @projects = Project.id_in(projects).paginate(...) or @projects = Project.paginate(...,:conditions => { :id => projects }) Both of which will generate an appropriate SQL condition.> #RENDER PARTIAL > render :partial => ''projects/results'', :locals => {:projects => > projects} > endWhy are you rendering a partial here? The standard behavior is to stash the returned objects in a controller instance variable (@projects here) and then use them in the view, either by the implicit render (which would go to projects/search.html.erb here) or by an explicit render, for example: render :action => ''results'' Hope this helps! --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.