ruby-on-rails - 分配父母 child_id 的表单
问题描述
我认为这个解决方案是相当可以实现的......我只是还没能实现它 -
我有一个有多个地区的应用程序。我还有一个名为 Person 的模型。一个人可以是候选人——在这种情况下,他们属于该地区,或者他们是现任者,在这种情况下,该地区属于他们。
这是我现有的协会:
class Person < ApplicationRecord
belongs_to :district, inverse_of: :candidates, class_name: 'District', optional: true,
has_one :incumbent_district, inverse_of: :incumbent, class_name: 'District', optional: true
class District < ApplicationRecord
has_many :candidates, inverse_of: :district, class_name: 'Person'
belongs_to :incumbent, inverse_of: :incumbent_district, class_name: 'Person', foreign_key: 'incumbent_id', optional: true
在我创建一个人的表单上,如果他们是现任者,我想要一个下拉菜单来选择哪个区是他们的——最终编辑区表以修改“incumbent_id”字段以将其设置为我目前正在创建的人。我知道如何在表单上设置父级,但我一直在弄清楚子部分。
任何指导将不胜感激!