# Bad query?

**URL:** <https://www.ruby-forum.com/t/bad-query/99575>\
**Category:** Ruby\
**Created:** [July 24, 2006, 7:07am UTC](https://www.ruby-forum.com/t/bad-query/99575 "2006-07-24T07:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raffir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@raffir](https://www.ruby-forum.com/u/raffir)\
**Post date:** [July 24, 2006, 7:07am UTC](https://www.ruby-forum.com/t/bad-query/99575/1 "2006-07-24T07:07:26Z")

</div>

I have a find() query inside a for loop. Basically, I want the loop to  
go through all elements in User.accounts and find all elements in  
Accounts that have the same name as each element (to see other users  
who have accounts with the same name).

Right now my code is as follows:

```
<table>
<%@session['user'].accounts.each do |i| %>
<%Accounts.find(:all, :conditions => ["coursenumber LIKE ?",

```

“%#{i.coursenumber}%”]).each do |account| %\>

```
        <tr>
        <td><%= account.coursename %></td>
        <td><%= account.coursenumber %></td>
        <td><%= account.user_id%></td>
		<td><%= account.first_name%> <%= account.last_name%></td>
		<td><%= link_to 'Show', :controller=>'user', :action=>'userdetails',

```

:id=\> account.user\_id %\>

```
        <%end%>
<% end %>

```

This has the effect of outputting every single account in the Accounts  
table. I’d like to restrict it to just outputting those accounts with  
the same course name/number as each one of the User’s.

Thanks a lot,

- Raffi.

---

<div class="post-metadata">

**Author:** ![raffir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@raffir](https://www.ruby-forum.com/u/raffir)\
**Post date:** [July 24, 2006, 10:44am UTC](https://www.ruby-forum.com/t/bad-query/99575/2 "2006-07-24T10:44:21Z")

</div>

On 7/24/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <%Accounts.find(:all, :conditions => ["coursenumber LIKE ?",
> 
> ```
> 
> :id=\> account.user\_id %\>
> 
> Thanks a lot,
> 
> - Raffi.

Hmm, looks like a Rails problem, although there are a lot of nice  
people  
around who might be willing to help, sorry I cannot :(, I think you  
should  
post to the Rails Mailing List.

> **[Ruby on Rails](https://rubyonrails.org)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![raffir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@raffir](https://www.ruby-forum.com/u/raffir)\
**Post date:** [July 24, 2006, 11:57am UTC](https://www.ruby-forum.com/t/bad-query/99575/3 "2006-07-24T11:57:06Z")

</div>

Try to fiddle with the condition… what are your course numbers like?  
J.

---

<div class="post-metadata">

**Author:** ![raffir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@raffir](https://www.ruby-forum.com/u/raffir)\
**Post date:** [July 24, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/bad-query/99575/4 "2006-07-24T13:09:07Z")

</div>

On 7/24/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <%Accounts.find(:all, :conditions => ["coursenumber LIKE ?",
> 
> ```
> 
> :id=\> account.user\_id %\>
> 
> Thanks a lot,
> 
> - Raffi.

You could simplify this a lot by using a db IN condition.

Does this need to be a LIKE condition or are the course numbers in fact  
the  
same.

Assuming that they are the same

\<% course\_numbers = session[‘user’].accounts.map { |a| a.coursenumber  
}.uniq  
%\>  
\<% Account.find( :all, :conditions =\> [ “coursenumber IN (?)”,  
course\_numbers] ).each do |acc| %\>  
…  
\<% end %\>

If they really do need a LIKE condition then I would only want to hit  
the db  
once. Who knows how many accounts there may be. You may have a better  
time  
with the ez\_where plugin by Ezra ( [http://brainspl.at](http://brainspl.at) ) but one way  
could  
be.

\<% course\_numbers = session[‘user’].accounts.map { |a| a.coursenumber  
}.uniq  
%\>  
\<% course\_query = course\_numbers.inject([]) { |arr,cn| arr \<\<  
"coursenumber  
LIKE ‘%#{coursenumber}%’ "} %\>  
\<% Account.find(:all, :conditions =\> course\_query.join( " OR " ) ).each  
do  
|acc| %\>  
…  
\<% end %\>

This is probably not too good to have this logic in your view anyway  
tho.  
Perhaps you should make this into a class method for account

in account.rb

def Account.find\_similar\_accounts( \*accounts )  
accounts = accounts.flatten  
course\_numbers = accounts.map { |a| a.coursenumber }.uniq  
Account.find(:all, :conditions =\> [ “coursenumber IN (?)”,  
course\_numbers]  
)  
end

Then in your view

\<% Account.find\_similar\_accounts( session[‘user’].accounts ).each do  
|acc|  
%\>  
…  
\<% end %\>

I hope I haven’t misunderstood your problem too badly…
