[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Showing similar values in a table

raffir

7/23/2006 5:18:00 AM

I'm working on a project in which Accounts belong to a User.

I'd like the User's Welcome page to show who else has the same Accounts
(based on account name) as that user.

What I tried to do was place a SQL query inside a for loop, such that
the loop would go through the session User's accounts and check, in
turn, for all similarly named accounts in the Accounts table.

I think that there must be something wrong with my code; right now,
it's as follows:

<table>
<%@session['user'].accounts.each do |i| %>
<%Accounts.find(:all, conditions = ["coursename LIKE ?",
"%#{i.coursename}%"])%>
<tr>
<td><%= account.name %></td>
<td><%= account.number %></td>
<td><%= account.user_id%></td>
</tr>
</table>
<%end%>

<% end %>


I'm new to Ruby, so I think syntax is the most likely culprit here.

Thoughts?


Thanks a lot,

- Raffi.

4 Answers

Jano Svitok

7/23/2006 9:44:00 AM

0

Hi,

On 7/23/06, raffir@gmail.com <raffir@gmail.com> wrote:
> I'm working on a project in which Accounts belong to a User.
> <table>
> <%@session['user'].accounts.each do |i| %>
> <%Accounts.find(:all, conditions = ["coursename LIKE ?",
> "%#{i.coursename}%"])%>

You're missing second 'each' here
<%Accounts.find(:all, conditions = ["coursename LIKE ?",
"%#{i.coursename}%"]).each do |account| %>

> <tr>
> <td><%= account.name %></td>
> <td><%= account.number %></td>
> <td><%= account.user_id%></td>
> </tr>
> </table>
> <%end%>
>
> <% end %>

raffir

7/23/2006 3:30:00 PM

0

Unfortunately, while it's now showing accounts, it's now showing every
single account. I'd just like it to show accounts with the same name as
the session user's.

Thanks,

- Raffi.


Jan Svitok wrote:
> Hi,
>
> On 7/23/06, raffir@gmail.com <raffir@gmail.com> wrote:
> > I'm working on a project in which Accounts belong to a User.
> > <table>
> > <%@session['user'].accounts.each do |i| %>
> > <%Accounts.find(:all, conditions = ["coursename LIKE ?",
> > "%#{i.coursename}%"])%>
>
> You're missing second 'each' here
> <%Accounts.find(:all, conditions = ["coursename LIKE ?",
> "%#{i.coursename}%"]).each do |account| %>
>
> > <tr>
> > <td><%= account.name %></td>
> > <td><%= account.number %></td>
> > <td><%= account.user_id%></td>
> > </tr>
> > </table>
> > <%end%>
> >
> > <% end %>

raffir

7/23/2006 3:53:00 PM

0

Fixed it!

The line was missing a colon. I changed it to:

<%Accounts.find(:all, :conditions => ["coursename LIKE ?",
"%#{i.coursename}%"]).each do |account| %>


A question about the SQL query: how do I add another clause? For
example, what if I'd like to search by coursename (as above) and
coursenumber?

Thanks,

- Raffi.

raffir

7/23/2006 7:30:00 PM

0

Mysteriously stopped working again.

Code is as follows:

<h3><u>Who's in my courses?</u></h3>
<table>
<%@session['user'].accounts.each do |i| %>
<%Accounts.find(:all, :conditions => ["coursename LIKE ?",
"%#{i.coursename}%"]).each do |account| %>

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

<%end%>
<% end %>
</table>