cvdv
cvdv

Reputation: 2932

do join tables need to use conventional naming in rails?

say I have company and employee tables, does my join table have to be called companies_employees? I don't see anything about this on the rails documentation.

Upvotes: 1

Views: 48

Answers (2)

Thanh
Thanh

Reputation: 8604

Name of join table can be changed. Here is document of has_and_belongs_to_many.

If the default name of the join table, based on lexical ordering, is not what you want, you can use the :join_table option to override the default.

So, you can change it like this:

# Company model
has_and_belongs_to_many :employees, join_table: "comp_emps"

# Employee model
has_and_belongs_to_many :companies, join_table: "comp_emps"

Upvotes: 1

David Aldridge
David Aldridge

Reputation: 52386

No, in fact none of the tables have to be named a particular way -- that's just convention. You can override the table name for any model, but usually you would do because you have a legacy schema.

And similarly, if you have a model that belongs to both Company and Employee it doesn't have to be named CompanyEmployee (conventional table name would be company_employees) or EmployeeCompany (conventional table name employee_companies, but it often makes sense to.

Upvotes: 1

Related Questions