Forum: Ruby on Rails Listing elements from a table

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.
Stephen F. (Guest)
on 2009-04-22 16:11
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.
Robert W. (Guest)
on 2009-04-22 23:41
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/Na...

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.
Stephen F. (Guest)
on 2009-04-22 23:48
>
> 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!
Robert W. (Guest)
on 2009-04-23 00:26
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/inde...
This topic is locked and can not be replied to.