I’m trying to work out the best method for the following relationship.
I have a User who is assigned to a Company, however the User can be
assigned to more than 1 Company. And, the Company can have Many Users…
Any suggestions?
I’m trying to work out the best method for the following relationship.
I have a User who is assigned to a Company, however the User can be
assigned to more than 1 Company. And, the Company can have Many Users…
Any suggestions?
What you are describing is a has_and_belongs_to_many relationship, which
uses a separate table to link the 2 models:
class User << ActiveRecord::Base
has_and_belongs_to_many :company
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
You will need to generate a migration:
class CreateCompaniesUsersJoinTable < ActiveRecord::Migration
def self.up
create_table :companies_users, :id => false do |t|
t.integer :company_id
t.integer :user_id
end
end
def self.down
drop_table :companies_users
end
end
More:
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs