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

The Archive Base Latest Questions

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

I have isolated the problem in the second block of code below (if you

  • 0

I have isolated the problem in the second block of code below (if you don’t want all the details): I can create new users from the account model. I can’t assign those users roles from the accounts model. I am using fields_for, this method does not work when I attempt to assign the role_ids to the roles model. My db is set up in the following way:

  1. Account model has_many :users

  2. User model has_and_belongs_to_many :roles,
    belongs_to :accounts

  3. Roles model has_and_belongs_to_many :users

views/accounts/new.html.erb

<% for user in @account.users %>
 <% fields_for "account[user_attributes][]", user do |account_fields| %>
      <p>Login  : <%= account_fields.text_field :login %>
      <p>Email  : <%= account_fields.text_field :email %>
      <p>Password  : <%= account_fields.password_field :password %>
      <p>Confirm Password  : <%= account_fields.password_field :password_confirmation %>
      <%= account_fields.hidden_field :account_id, :value => :id %>

    <% end %>
<% end %>

<% for role in @account.users.first.roles %>
<% fields_for "account[role_attributes]]", role do |role_fields| %>
  <%= role_fields.hidden_field :role_ids, :value => '[1]' %>
 <% end %>
<% end%>

Associated setter methods in the account.rb model: using the stacktrace I have isolated the problem to “undefined method `roles’ for #” on line 34, marked below:

def user_attributes=(user_attributes)
   user_attributes.each do |attributes|
   users.build(attributes)
   end
end

def role_attributes=(role_attributes)
    role_attributes.each do |attributes|
    users.roles.build(attributes)   #error here (stacktrace)
  end
end

finally, within the accounts controller I build the users and roles in memory:

 def new
    @account = Account.new
    1.times { @account.users.build }
    1.times { @account.users.first.roles.build }

The @accounts.users.first.roles.build proof from consol:

>> account
=> #<Account id: 1, subdomain: "justinzollars", created_at: "2010-02-08 14:41:13", updated_at: "2010-02-08 14:41:13">
>> account.users
=> [#<User id: 13, login: "jayz", email: "jz@jz.com", crypted_password: "f9a3d618fc650d285a90f9775508c13784891b97", salt: "f497a7dd909b695caff1f6310e710245615d55b6", created_at: "2010-02-08 20:25:48", updated_at: "2010-02-08 20:25:48", remember_token: nil, remember_token_expires_at: nil, account_id: 1>, #<User id: 16, login: "jasonwade23", email: "jasonwade@gmail.com", crypted_password: "06581b47cfac7a529773d61dc0b1d5d6c0da6c08", salt: "93f8b99cd9da60b904d553fcc7843bfb66352c3e", created_at: "2010-02-13 07:46:14", updated_at: "2010-02-13 07:46:14", remember_token: nil, remember_token_expires_at: nil, account_id: 1>]
>> account.users.first
=> #<User id: 13, login: "jayz", email: "jz@jz.com", crypted_password: "f9a3d618fc650d285a90f9775508c13784891b97", salt: "f497a7dd909b695caff1f6310e710245615d55b6", created_at: "2010-02-08 20:25:48", updated_at: "2010-02-08 20:25:48", remember_token: nil, remember_token_expires_at: nil, account_id: 1>
>> account.users.first.roles
=> [#<Role id: 1, name: "admin">, #<Role id: 2, name: "alt">]
>> 
  • 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:11:21+00:00Added an answer on May 13, 2026 at 4:11 pm

    You should use accepts_nested_attributes_for, so in models:

    # Account model
    accepts_nested_attributes_for :users
    
    # User model
    accepts_nested_attributes_for :roles
    

    Then you should remove user_attributes= and role_attributes= methods.
    Your form should look like this:

    <% form_for @account do |f| %>
       <% fields_for :users do |u| %>
          ... # user fields
          <% fields_for :roles do |r| %>
             ... # role fields
          <% end %>
       <% end %>
       <%= f.submit 'Save' %>
    <% end %>
    

    It will automaticaly iterate over all users associated with account and all roles associated with user.

    For more details read here.

    EDIT:
    You can assign roles to user, in controller:

    role = Roles.find(some_id)
    @user.roles << role
    

    or

    @user.role_ids = [2, 4, 6]
    

    However I’m not sure how to add roles with has_and_belongs_to_many association. You are using @user.roles.build method which will create new role and associate it with user. If you want to add a role you should do it with one of two examples above. But how to create a from for it? I don’t know. I would add a model for users_roles table and add to user model:

    has_many :users_roles
    accepts_nested_attributes_for :users_roles # you should add here also some :reject_if
    

    Then insted of fields_for :roles I would add in form:

    <% fields_for :users_roles do |ur| %>
      <%= ur.select :role_id, Role.all.collect {|r| [r.name, r.id] }, {:include_blank => true} %>
    <% end %>
    

    Then in your controller you should add something like 3.times { @user.users_roles.build }. If you want to have nice “Add” and “Remove” links that create for you new role with javascript, take a look here – it is really nice example how to handle it.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.