【发布时间】:2018-06-21 06:53:08
【问题描述】:
我有模型之间的显示关系。问题是,模型 Formulari 可以属于一个主题,或者属于一个区域,或者属于一个主题,或者属于 Subthema。这就是我使用可选选项的原因:true
class Formulari < ApplicationRecord
belongs_to :topic, optional: true
belongs_to :area, optional: true
belongs_to :thema, optional: true
belongs_to :subthema, optional: true
### how to make this work?
### it just uses the last one
has_one :user, through: :area
has_one :user, through: :thema
end
我想访问@formulari.user,所以我需要通过关系使用has_one,但是由于我有路径选项,所以很棘手。如果我通过关系写了多个 has_one,rails 只取其中一个。
我不知道如何通过与模型的关系来告诉 rails,多个 has_one。
任何帮助将不胜感激。 谢谢
【问题讨论】:
-
我认为你可以在这里利用父子关系的自我参照模型
标签: ruby-on-rails activerecord