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 9110871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:22:17+00:00 2026-06-17T03:22:17+00:00

and thanks in advance for any help offered here. I’m new to rails development

  • 0

and thanks in advance for any help offered here. I’m new to rails development and stackoverflow actually. I’m creating a many to many relationship and having trouble with some basics. I’m wanting to do a User and Groups relationship.

Models:

    class User < ActiveRecord::Base
      attr_accessible :name
      has_and_belongs_to_many :groups
    end

    class Group < ActiveRecord::Base
      attr_accessible :name
      has_and_belongs_to_many :users
    end

db/migrate create_groups_users_join

class CreateGroupsUsersJoin < ActiveRecord::Migration
  def self.up
    create_table 'groups_users', :id => false do |t|
      t.column 'group_id', :integer
      t.column 'user_id', :integer
     end
   end
 def self.down
   drop_table 'groups_users'
 end
end

I created a dropdown on my users form with this code.

true }) %>

controllers

def edit
  @user = User.find(params[:id])
  @groups = Group.all
  ...
end
  def new
  @user = User.new
  @groups = Group.all
  ...
end

I’m lost now as far as what to add to the controller to update the join table on creations/edits/updates. Can anyone explain what I need to change to make this happen? Thank you.

Here’s the Error output I am getting now:

 {"utf8"=>"✓",
    "authenticity_token"=>"pgyajCT23qQVMuS+MQgG6E7M7Q8AWjfGaYbe3q7QDSA=",
    "group"=>{"group_id"=>"1"},
    "user"=>{"name"=>"ymudfg"},
    "commit"=>"Create User"}
  • 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-06-17T03:22:18+00:00Added an answer on June 17, 2026 at 3:22 am

    You can add user to group by doing this in your controller:

       user = User.find(params[:uid])
       group = Group.find(params[:gid])
    
       group.users <<  user  #Adding user to group
    

    For assigning group to user:

       user.groups << group
    

    EDIT: As per your comment

      class UsersController < ApplicationController
        def new
          @user = User.new
          @group = Group.all
        end
    
        def create
          @user = User.new(params[:user])
          @group = Group.find(params[:group][:group_id]) # As per log
          if @user.save
            @group.users << @user
          else
            render :new
          end
        end
    
        #Similarly you can implement edit
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new at this so thanks in advance for any help here. I'm just
Thanks in advance for any help. I am new to python, but not particularly
Bit of a jQuery/JavaScript beginner here so thanks in advance for any help you
First, many thanks in advance for any help. I'm a complete novice with programming
Thanks in advance for any help you can provide! For my Ruby on Rails
Thanks in advance for any help. :) Ok, here's my problem. Simplified version of
Thanks in advance for any help you can provide on this issue! I manage
I am using c# .net. Thanks in advance for any help. I have searched
Thanks for any help in advance, I can't wrap my SQL skills around this
Thanks in advance for any help you can provide! I've reviewed similar articles on

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.