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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:24:24+00:00 2026-05-13T16:24:24+00:00

*Note to view all code, follow the link I am creating new accounts. Each

  • 0

*Note to view all code, follow the link

I am creating new “accounts”. Each new Account have_many :users. Users have_and_belong_to_many roles. I am trying to assign EXISTING roles to new users, in the new account form. Typically an easy process however…

The have_and_belong_to_many association between Users and Roles implies that users and roles are associated by a join table. I don’t have a model to work with. So I am not sure how to go about coding this. I’m able to create new Roles, using methods outlined here (in the answer) but I am clueless as to assign existing Roles to new users through the account model.

The SQl equivalent would be something like this: SQL (0.1ms) INSERT INTO "roles_users" ("role_id", "user_id") VALUES (6, 29)Where I need 29 to function like the ID of the new user created in rails i.e. VAULES (6, ID)

Current Code: (creates a new role)

<% f.fields_for :users do |builder| %>
<%= render 'user_fields', :f => builder %>
<p>Login  : <%= f.text_field :login %>
<p>Email  : <%= f.text_field :email %>
<p>Password  : <%= f.password_field :password %>
<p>Confirm Password  : <%= f.password_field :password_confirmation %>
<%= f.hidden_field :account_id, :value => :id %>


<% f.fields_for :roles do |builder| %>
     <%= builder.hidden_field :name, :value => 'test' %>
<% end %>

Account.rb

has_many :users
accepts_nested_attributes_for :users

User.rb

has_and_belongs_to_many :roles
accepts_nested_attributes_for :roles

accounts_controller.rb

def new
    @account = Account.new
    #builds user and roles in memory
    1.times do
      user =  @account.users.build 
    1.times { user.roles.build }
  end
  • 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-13T16:24:24+00:00Added an answer on May 13, 2026 at 4:24 pm

    The problem is you’re using accepts_nested_fields for when you shouldn’t be. It’s used to create and destroy items of the referenced model from the calling model. You want to manage the associations, from the user model, not the roles themselves.

    Here’s what’s got to be done:

    1. remove accepts_nested_attributes_for :roles from user.rb (optional, but recommended)
    2. Assign to role_ids field in the user creation form.

    This view should illustrated the point:

    <% f.fields_for :users do |builder| %>
    <p>Login  : <%= f.text_field :login %>
    <p>Email  : <%= f.text_field :email %>
    <p>Password  : <%= f.password_field :password %>
    <p>Confirm Password  : <%= f.password_field :password_confirmation %>
    <%= f.hidden_field :account_id, :value => :id %>
    
    <%= f.collection_select :role_ids, Role.all, :id, :name, {}, :multiple => true %>
    
    <% end %>
    

    Without changing anything else, Rails will populate the join model on create instead of modifying existing Roles or creating new ones.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this is not really an answer to your question, but… May 14, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer If you primary key is composite, your mapping should reflect… May 14, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer It's a good practice to wrap keys in quotes, even… May 14, 2026 at 6:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.