# Listing elements from a table

**URL:** <https://www.ruby-forum.com/t/listing-elements-from-a-table/166163>\
**Category:** Rails\
**Created:** [April 22, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/listing-elements-from-a-table/166163 "2009-04-22T14:11:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 22, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/listing-elements-from-a-table/166163/1 "2009-04-22T14:11:48Z")

</div>

I am writing an application where users can become members of different  
groups. On each group page I want to list all the members of the group  
by user name and also the groups to which they belong. My group table  
consistes of id, name, description. My membership table consists of id,  
user\_id, group\_id, role.

How would I define the members and groups in my controller to do this  
and also how could I list them in my views?

Thanks in advance,

Steve.

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 22, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/listing-elements-from-a-table/166163/2 "2009-04-22T21:48:52Z")

</div>

> Those are some of the things I can think of that should help you build  
> your group show pages. Hope this gives you some ideas on how to proceed.

Thanks a million. Thats helps me a great deal. Sometimes you cant see  
the wood for the trees!

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 22, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/listing-elements-from-a-table/166163/3 "2009-04-22T21:41:06Z")

</div>

Stephen F. wrote:

> How would I define the members and groups in my controller to do this  
> and also how could I list them in my views?

## groups\_controller.rb

def show  
@group = Group.find(params[:id]);  
…  
…  
end

With groups you can get memberships:  
memberships = @group.memberships

From memberships you get users:  
memberships.each do |membership|  
membership.user  
end

There are ways to get your ordering by taking advantage of named\_scopes:  
[http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#M002120](http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#M002120)

Those are some of the things I can think of that should help you build  
your group show pages. Hope this gives you some ideas on how to proceed.

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 22, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/listing-elements-from-a-table/166163/4 "2009-04-22T22:26:58Z")

</div>

Stephen F. wrote:

> > Those are some of the things I can think of that should help you build  
> > your group show pages. Hope this gives you some ideas on how to proceed.
> 
> Thanks a million. Thats helps me a great deal. Sometimes you cant see  
> the wood for the trees!

Also don’t forget about default\_scope. That can be very useful for  
providing default sort orderings to your models:

[http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002363&name=default\_scope](http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002363&name=default_scope)
