Find by looping thru array

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

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,

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