Forum: Ruby on Rails Model Each Do Looping Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f4b8f42cd9b0ea56fb3fbff15a6ae5d?d=identicon&s=25 bradigan (Guest)
on 2007-08-01 17:35
(Received via mailing list)
I have this strange loop going on.
Here's my Data for instance in a Table called GROUPS
account_id = 1
group_name = Acme
account_id = 1
group_name = Disney
account_id = 1
group_name = Pixar

/group/list.rhtml
<%= render :partial => "grouplist", :collection => @groups %>

/group/_grouplist.rhtml
 <% @groups.each do |group| %>
  <li><%= group.group_name %></li>
<% end %>

../group_controller.rb
def list
  @groups = Group.find_all_by_account_id(session[:user_id]);
  ...


Now, my view renders it like so:

- Acme
- Disney
- Pixar
- Acme
- Disney
- Pixar
- Acme
- Disney
- Pixar

I just want it to display only once
- Acme
- Disney
- Pixar

I'm sure its so simple, but I am banging my head to find out!!!
B4084fdc8c0512d3402387a87b008035?d=identicon&s=25 Preston Lee (Guest)
on 2007-08-01 21:30
(Received via mailing list)
On Aug 1, 2007, at 8:32 AM, bradigan wrote:
> /group/list.rhtml
> <%= render :partial => "grouplist", :collection => @groups %>

Using ":collection" makes this line automatically do the looping for
you.

> /group/_grouplist.rhtml
>  <% @groups.each do |group| %>
>   <li><%= group.group_name %></li>
> <% end %>

So you don't need the loop in the partial. Take out the loop and I
bet that'll fix your issue. I've made this mistake accidentally
before too :)
This topic is locked and can not be replied to.