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

  • Home
  • SEARCH
  • 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 3429990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:09:00+00:00 2026-05-18T07:09:00+00:00

I am trying to save to a join table in a habtm relationship, but

  • 0

I am trying to save to a join table in a habtm relationship, but I am having problems.

From my view, I pass in a group id with:

<%= link_to "Create New User", new_user_url(:group => 1) %>

 

# User model (user.rb)
class User < ActiveRecord::Base  
  has_and_belongs_to_many :user_groups
  accepts_nested_attributes_for :user_groups
end

 

# UserGroups model (user_groups.rb)
class UserGroup < ActiveRecord::Base
  has_and_belongs_to_many :users
end

 

# users_controller.rb
def new
  @user = User.new(:user_group_ids => params[:group])
end

in the new user view, i have access to the User.user_groups object, however when i submit the form, not only does it not save into my join table (user_groups_users), but the object is no longer there. all the other objects & attributes of my User object are persistent except for the user group.

i just started learning rails, so maybe i am missing something conceptually here, but i have been really struggling with this.

  • 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-18T07:09:01+00:00Added an answer on May 18, 2026 at 7:09 am

    Instead of using accepts_nested_attributes_for, have you considered just adding the user to the group in your controller? That way you don’t need to pass user_group_id back and forth.

    In users_controller.rb:

    def create
      @user = User.new params[:user]
      @user.user_groups << UserGroup.find(group_id_you_wanted)
    end
    

    This way you’ll also stop people from doctoring the form and adding themselves to whichever group they wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to save a row in my settings table, it works all fine
Im trying to save a simple script in netbeans to htdocs, but i only
I'm trying to save some data into an array but unfortunately all the data
I am trying to save multiple image using this code. But instead its saving
I am trying to save an user. The user entity and group entity has
I am trying to create and save a thumbnail image when saving the original
I am trying to retrieve properties which has multiple amenities. But the table structure
I am trying to save a pic taken from the iphone camera to core
I'm trying to save myself from performing the old copy and past trick when
I´m trying to create a VIEW in SQL Server 2005. The SQL code is

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.