The relationship between an employee and a company can be defined as "An employee can be in only one company but one company may have many employees". So now we will be adding an association between an employee and the company model. After we make entries for the company in the company model, we would be able to see the company select box populated in the employee form.
The relationship between the two models is defined in the employee.rb
file as:
belongs_to :company_id, 'Company'
Here, Company
corresponds to the model name and
company_id
corresponds to the foreign key.
Since at present we have the company
field instead of company_id
in the employee model, we will rename company
to company_id
.
To retrieve all the companies, which are stored in the Company model, we need to add this line in the new action of the employee_controller
:
@companies = Company.find(:all)
The find
command is provided by Rhom, which is used to form...