Answer the question
In order to leave comments, you need to log in
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
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
class User < ActiveRecord::Base
has_and_belongs_to_many :assistants
end
class Assistant < ActiveRecord::Base
has_and_belongs_to_many :users
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question