E
E
eldar_web2015-05-28 14:17:55
Ruby on Rails
eldar_web, 2015-05-28 14:17:55

What connection and how to apply it correctly in Rails in this case?

There is a Users table.
I want to create a 'boss-employee' functionality where one boss will have multiple assistants, but one assistant can have multiple bosses.
And for this, as I understand it, I must also create an Assistant table, with the manager_id fields - the boss's id, and assistant_id - the assistant's id.
But here's how to apply links??? Or is there another option and how it is done (there is no need for a self-joining connection here) ???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cortc, 2015-05-28
@Cortc

has_and_belongs_to_many or has_many through

K
kkrieger, 2015-05-28
@kkrieger

class CreateUserAndAssistant < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps
    end
 
    create_table :assistants do |t|
      t.string :name
      t.timestamps
    end
 
    create_table :users_assistants, id: false do |t|
      t.belongs_to :user, index: true
      t.belongs_to :assistant, index: true
    end
  end
end

Model
class User < ActiveRecord::Base
  has_and_belongs_to_many :assistants
end
 
class Assistant < ActiveRecord::Base
  has_and_belongs_to_many :users
end

c504a3d633ae4701b82eaf7eaf7f7d97.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question