Listing elements from a table


#1

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.


#2

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!


#3

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

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.


#4

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