Model Each Do Looping Problem


#1

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| %>

  • <%= group.group_name %>
  • <% 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!!!


    #2

    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| %>

  • <%= group.group_name %>
  • <% 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 :slight_smile: