Take the 2-minute tour ×

As an RoR newbie, I greatly appreciate any/all assistance in advance! I'm confusing myself when attempting to create the models needed for the following.

I have 3 objects: Users, Organizations and Roles. Users can belong to one or more Organizations, but will have only 1 role per organization. For example:

Jon | Org1 | Owner

Jon | Org2 | Employee

Bob | Org1 | Employee

Bob | Org2 | Owner

How would I set this up in my models (has_many, through =>?) and also so that when I have an edit form, I'm able to update the user information, their Organizations and Roles all from within the same form? Note: Not sure it pertinent, but I only intend to allow those that are owners to edit their Organization.

Thanks again!

EDIT Below is what I've got, plus an error I now receive:


class User < ActiveRecord::Base
    has_many :org_roles 
    has_many :orgs, :through => :org_roles
    accepts_nested_attributes_for :orgs, :allow_destroy => true
    has_one :user_detail
    has_one :user_address

  attr_accessible :orgs


class Org < ActiveRecord::Base


class Role < ActiveRecord::Base


class OrgRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :org
  belongs_to :role

  validates_presence_of   :user, :org, :role
  validates_uniqueness_of :org_id, :scope => :user_id


 #user form info above...
    <%=f.fields_for :orgs do |ff| %>  
      <div>Your Organization Name:<br />
      <%= ff.text_field :name%></div>
    <% end %>

Error Message:

Can't mass-assign protected attributes: orgs_attributes


Added :orgs_attributes to my user model, attr_accessible

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I would create a fourth model called UserOrganization which has the following attributes: user_id, organization_id and role_id. Within the UserOrganization model I would have the following:

class UserOrganization < ActiveRecord::Base
  belongs_to :user
  belongs_to :organization
  belongs_to :role

  validates_presence_of   :user, :organization, :role
  validates_uniqueness_of :organization_id, :scope => :user_id

What we have there would satisfy your criteria of users being able to belong to many organizations, but only a maximum of once per organization, and for each of those associations they must have a role present.

The associations for User, Organization and Role should be fairly straight forward with that implementation (user has many user organisations). If you want to get organisations directly through the user model you could also have has_many :organizations, :through => :user_organizations.

Also for your question about the edit form I'd recommend you have a read about accepts_nested_attributes_for:




share|improve this answer
Thank you! I appreciate it –  user464180 Feb 27 '12 at 3:45
I added my code and an error I'm receiving. If you have time and could review, I'd sincerely appreciate it. Thanks again. –  user464180 Feb 27 '12 at 13:34
I got it. Thanks for helping me get on the right path towards a solution. –  user464180 Feb 27 '12 at 14:09

Your Answer


