我有两个模型,部门和职员,如下所示:
class Department < ApplicationRecord
end
class Staff < ApplicationRecord
end
在创建新员工时,我会在下拉列表中列出各个部门。
<%= form.collection_select :department, Department.order(:name),:name,:name,
include_blank: true %>
在人员表中,我没有department_id
列,而有department
。从下拉列表中选择时,我尝试保存部门名称而不是Department_id。从上面的下拉菜单中,我仍在保存ID,这不是我想要的。
是否可以保存名称而不是department_id?
答案 0 :(得分:0)
这听起来像是不好的做法。有了您提供的信息,您就不需要Department
表。
如果您只是想将部门另存为string
,例如,建议您从constant
呈现集合。
在模型Staff
内
DEPARTMENTS = ['HR', 'IT', 'OFFICE']
并在您看来
f.input :department, collection: DEPARTMENTS
我希望它会有所帮助。