ruby - 多个 Has_Many 与相同模型但不同列表的关系
问题描述
我正在努力建立一个一对多的关系,顺便说一下,父对象与子对象有3种不同的关系。孩子只能与父母处于这 3 种关系中的一种。
我有一种感觉,我把它混合了一点,会很好,如果有人可以为这个话题带来一些启发,那么建立这些关系的最佳实践方法是什么。我对rails比较陌生。干杯
我的迁移
add_reference :child_elements, :child_list_a, foreign_key: { to_table: :parent_elements }
add_reference :child_elements, :child_list_b, foreign_key: { to_table: :parent_elements }
add_reference :child_elements, :child_list_c, foreign_key: { to_table: :parent_elements }
课程:
Child:
belongs_to :child_list_a, class_name: "ParentElement"
belongs_to :child_list_b, class_name: "ParentElement"
belongs_to :child_list_c, class_name: "ParentElement"
Parent:
has_many :child_elements_a, inverse_of: "child_list_a"
has_many :child_elements_b, inverse_of: "child_list_b"
has_many :child_elements_c, inverse_of: "child_list_c"
解决方案
推荐阅读
- swiftui - 从按钮显示新视图按下 Swift UI
- javascript - 在javascript中使用reduce()通过多个键对数组的值进行分组
- ruby-on-rails - JSON 用于将多个记录传递到 POST 请求到 Rails API 创建操作?
- asp.net-mvc - HttpException:在控制器上找不到公共操作方法“ListCheckListType”
- machine-learning - 我怎样才能让 spacy 不产生 -PRON- lemma?
- ios - 有没有办法在 IOS 上使用 SVG 中的锚链接?
- python - Lambda 作为 Python 中的迭代器在第一次迭代时返回函数对象
- javascript - 无法在 reactJS App 中正确导入函数?
- python - 使用 .loc 修改 Pandas 数据框无法按预期工作
- python - 如何在 Django ORM 中进行单字符替换( _ )的通配符搜索?