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 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

Related Questions

Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note: This was posted when I was starting out C#. With 2014 knowledge, I
NOTE: I am not set on using VI, it is just the first thing
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note that I am not asking which to choose (MVC or MVP), but rather
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
NOTE : I mention the next couple of paragraphs as background. If you just
Note This is not a REBOL-specific question. You can answer it in any language.

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.