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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:15:18+00:00 2026-06-09T15:15:18+00:00

I have a bit of a complex model association: class Company < ActiveRecord::Base has_many

  • 0

I have a bit of a complex model association:

class Company < ActiveRecord::Base
  has_many :roles, :dependent => :destroy
  has_many :users, :through => :roles
end

class Role < ActiveRecord::Base

  belongs_to :user
  belongs_to :company
  attr_accessor :roles_attributes
  attr_accessible :roles_attributes, :active, :company_id, :role 
  validates_presence_of :company_id, :role
  validates_uniqueness_of :user_id, :scope => :company_id, :message => "Users may only have one role per company."
end

class User < ActiveRecord::Base
  has_many :roles, :dependent => :destroy
  accepts_nested_attributes_for :roles, :allow_destroy => true 
  has_many :companies, :through => :roles
end

The intent here is that a single user (email address) could login under different companies with different permissions (roles) per company.

I have users nested under company and my update controller works fine but now I can’t seem to get the new/create controller to work:

Controller:

  def new
    @user = User.new
    @role = @user.roles.build.company_id = session[:company_id]

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  def create

    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

and the View:

<%= simple_nested_form_for [:company, @user] do |f| %>
  <fieldset>
    <div class="form-horizontal">

    <%= f.input :email %>
    <%= f.input :name_first %>
    <%= f.input :name_last %>
    <%= f.input :title %>
    <%= f.input :phone %>
    <%= f.input :mobile %>

    <%= f.simple_fields_for :roles, @role do |role_form| %>
        <%= role_form.hidden_field :company_id %>
        <%= role_form.input :active %>
        <%= role_form.input :role, :collection => [ "Guest", "User", "Inspector", "Owner"] %></td>
    <% end %>
    <%= f.input :notes, :input_html => { :rows => 5, :cols => 70 } %>
    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', company_users_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

When I submit the form it fails silently except that I notice in the logs that “roles_attributes” is being passed as:

"roles_attributes"=>{"0"=>{"company_id"=>"2", "active"=>"1", "role"=>"Inspector"}}

which I think should be:

"roles_attributes"=>[{"company_id"=>"2", "active"=>"1", "role"=>"Inspector"}]

I must be missing something obvious.

  • 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-09T15:15:20+00:00Added an answer on June 9, 2026 at 3:15 pm

    Turns out it was has_secure_password. I had deleted the password fields and the validation was failing.

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

Sidebar

Related Questions

I have a bit more complex system... I have users model - hasMany Ads
A bit of a complex problem to describe here: I would like to have
Have a bit of confusion here. I added a few objects from a class
I have this models: class BillHeader(models.Model): billno = models.IntegerField(primary_key=True, blank=True) class BillData(models.Model): price =
looking for a bit of Ajax help here... I have quite a complex view
I have a bit complex linq2sql query, it doesn't contain any 'order by' statements,
The situation is a bit complex. I have a web container which controls another
This is a bit complex so I hope I word this right. I have
I have a little bit complex MySQL query for me and i can't figure
I have a bit of a complex issue with a CSS background. Check out

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.