On Jan 7, 11:03 am, John Mcleod <li...@ruby-forum.com> 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 LOST

You'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}
>   end

Why 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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to