-1

I'm looking to create the following models in Rails 5:

Industry
Department
JobTitle

Example Data:

Industry: Technology, Healthcare, Other
Department: Admin, Customer Support
JobTitle: Account Manager, Accountant

Industry is the highest-level, departments can belong to one or more industries and Job titles can belong to one or more departments.

I have the three models above created in Rails, what I don't have is, given an Industry and department, how do I get all the JobTitles?

How should I store this relationship and then how do I query to get it?

Should I create a 3 table join model like:

IndustriesDepartmentsJobTitles
id | industry_id | department_id | job_title_id

Would that be the right way to do it? If so, how would I then query for the job_titles give an industry_id & department_id?

halfer
  • 19,824
  • 17
  • 99
  • 186
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • 1
    Why not use two join tables? I guess you have HABTM relationship in both Industry-Department and Department-JobTitle Tables, if this is true you can easily access your models like: `job_titles = Industry.first.departments.select do |dep| dep.job_titles end` (This one is rough example). – Lun4i Aug 04 '17 at 05:37
  • 1
    Just consider that when you add `belongs_to`, `has_many` or `has_and_belongs_to_many`, it adds methods to access and modify your associated objects like: my_industry.departments (where my_industry is the Industry class object). – Lun4i Aug 04 '17 at 05:48

1 Answers1

2

If you use one table for both the relationship models, you'll end up having a lot of duplicated columns. It should probably be split into 2 tables.

This rails docs section and this have a good explanation of choosing the way to express this many-to-many relationship using HABTM vs has_many through:.

An industry can have multiple departments, and vice versa.

class Industry << ApplicationRecord
  has_and_belongs_to_many :departments
end

# Relationship table needed: departments_industries

class Department << ApplicationRecord
  has_and_belongs_to_many :industries
  has_and_belongs_to_many :job_titles
end

# Relationship table needed: departments_job_titles

class JobTitle << ApplicationRecord
  has_and_belongs_to_many :departments
end

Migration sample:

create_table :industries do |t|
  t.string :name
  t.timestamps
end

create_table :departments do |t|
  t.string :name
  t.timestamps
end

create_table :job_titles do |t|
  t.string :name
  t.timestamps
end

create_table :departments_industries do |t|
  t.belongs_to :industry, index: true
  t.belongs_to :department, index: true
  # ..
  t.timestamps
end

create_table :departments_job_titles do |t|
  t.belongs_to :department, index: true
  t.belongs_to :job_title, index: true
  # ..
  t.timestamps
end

Query:

tech = Industry.find_by(name: 'Technology')
department = tech.departments.find_by(name: 'Admin')
department.job_titles
Ho Man
  • 2,308
  • 11
  • 16
  • How do I then create a DepartmentsIndustries record? Do I need to create a model in rails DepartmentsIndustries ? – AnApprentice Aug 04 '17 at 12:39
  • 1
    Nope, you only create the intermediate model if you use `has_many :through` – Ho Man Aug 05 '17 at 12:25
  • 1
    As for assigning departments/industries you can do something like `tech.departments.build(name: 'department name')` or even `tech.departments = [department_1, department_2]` etc – Ho Man Aug 05 '17 at 12:26