Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6027373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:35:01+00:00 2026-05-23T04:35:01+00:00

Consider these two resources: user and group . Rules: A group is owned by

  • 0

Consider these two resources: user and group.

Rules:

  • A group is owned by an user;
  • A group contains many users;
  • A user can have many groups;
  • A user can attend to many groups;

What I have:

class User
  has_many :groups, :foreign_key => "owner_id"
  has_and_belongs_to_many :attended_groups,
                      :class_name => "Group",
                      :join_table => "groups_members",
                      :foreign_key => "member_id"
end

class Group
  belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"
  has_and_belongs_to_many :members, :class_name => "User",
                                 :join_table => "groups_members",
                                 :association_foreign_key => "member_id"
end

My question is: what is the best (elegant?) solution to add actions in group controller, and also routes to, while owner sees his group (and all members), let it see who is not there and maybe add it. Something like: /groups/1/add_member/2. Same thing for a user to add a group, while he sees its page.

I’ve managed to make it work, but I would like to see how it should be. The problem is too simple to have a solution that complicated as mine. Maybe the way I modeled the problem is not the best way too.

Just for the record, I’m a completely newbie to Rails.

Please comment! And thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:35:02+00:00Added an answer on May 23, 2026 at 4:35 am

    You probably want to do a nested resource pattern. It adheres to REST though, so you’re going to have to deal with the naming conventions for urls.

    resources :groups do
      resources :users
    end
    

    This is a good resource: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

    In order to add an existing user to an existing group I would probably define a new route.

    match '/groups/:group/users/add' => 'groups#add'
    

    And this in the controller

    @group = Group.find params[:group]
    @user = User.find params[:user]
    
    @group.users << @user if @user
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider I have a these two properties: public class Test { [Required(ErrorMessage = Please
Consider these two scenarios: a user presses a button in a view (e.g. Fulfill
I have some doubts over how Enumerators work, and LINQ. Consider these two simple
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider these two classes mapped to the same table. One is readonly via mutable=false.
I ran across a compilation issue today that baffled me. Consider these two container
Consider these 2 pieces of code (you can assume execeptionObj is of type Object
Consider these three mysql statements: select * from Users; select id, title, value from
Consider these two classes: public class A { B b; public A(B b) {
How does EmailProperty differ from StringProperty ? Consider these two examples: # example 1:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.