# Find by looping thru array

**URL:** <https://www.ruby-forum.com/t/find-by-looping-thru-array/181227>\
**Category:** Rails\
**Created:** [January 7, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/find-by-looping-thru-array/181227 "2010-01-07T17:03:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/find-by-looping-thru-array/181227/1 "2010-01-07T17:03:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/find-by-looping-thru-array/181227/2 "2010-01-07T18:10:15Z")

</div>

John M. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 7, 2010, 10:49pm UTC](https://www.ruby-forum.com/t/find-by-looping-thru-array/181227/3 "2010-01-07T22:49:51Z")

</div>

On Jan 7, 11:03Â am, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to a partial.  
> Â Â # assign each  
> Â Â @title.each do |project|  
> 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 J.
