1

How can i achieve this with rails? i want to make a join table with the same model, it is for colleagues so i want to have employee 1 with employee 2 maybe and 1 with 3... etc

so is this the best way to approach this in active records?

class Employee < ActiveRecords::Base
end

class Colleague < ActiveRecords::Base
   has_many :employees, :foreign_key => 'employee_id' 
   has_many :colleague, :foreign_key => 'employee_id' 
end

what you think? how should my migration be?

like this?

create_table :colleague do |t|
  t.integer :employee_id
  t.integer :colleague_id
end
Abdul Hamid
  • 168
  • 1
  • 7
  • 25

3 Answers3

1

You can use has_and_belongs_to_many.

For your example, this would be something like:

class Employee < ActiveRecord::Base
  has_and_belongs_to_many :colleagues
end

class Colleague < ActiveRecord::Base
  has_and_belongs_to_many :employees
end

That said, I imagine what you actually want is employee to be a self-referential model, as described here

Community
  • 1
  • 1
Sean
  • 333
  • 4
  • 9
  • actually yes i want a self referential model to employees so the colleague table would help me to see who are colleague of who, so if i use has_and_belongs_to_many do i have to create the table in the database? – Abdul Hamid Apr 19 '15 at 19:19
0

I would handle this with a has_many :through relationship. First, create the join table

class CreateColleagueships < ActiveRecord::Migration
  def change
    create_table :colleagueships do |t|
      t.references :employee, index: true
      t.references :colleague, index: true

      t.timestamps null: false
    end
  end
end

Setup the associations

class Colleagueship < ActiveRecord::Base
  belongs_to :employee
  belongs_to :colleague, :class_name => 'Employee'
end

class Employee < ActiveRecord::Base      
  has_many :colleagueships
  has_many :colleagues, :through => :colleagueships
  # ...
end

You can then 'add a colleague` via

@colleagueship = employee.colleagueships.build(:colleague_id => params[:colleague_id])
@colleagueship.save
Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
  • So my Employee table will have a colleague id now and the colleagueship will handle this colleague_id and the employee_id? this way will be posible to add many many colleagues? or my employee table shouldnt(and this is what i think) have a colleague_id? – Abdul Hamid Apr 19 '15 at 20:18
  • Your `Employee` table should **not** have a `colleauge_id` column. The `Colleagueship` join table has this information. Yes, you can add many colleagues with this setup. – Bart Jedrocha Apr 19 '15 at 20:22
0

If to think a bit differently, you could do this:

class Employee < ActiveRecord::Base
  belongs_to :employer, class_name: Employee
  has_many :employees, class_name: 'Employee', foreign_key: 'employer_id'
end

So, colleagues are usually self.employer.employees except for the case when employer is nil, so you'll have to write a simple method to handle this, something like:

def colleagues
  if employer.nil?
    Employee.none
  else
    employer.employees
  end
end

The good thing is, you don't need additional tables.

IvanSelivanov
  • 710
  • 5
  • 15