有人可以解释数据如何以多态关联存储。
例如,如果我有评论,案例,案例步骤表。 评论表是链接: belongs_to :user, class_name: 'User', foreign_key: 'user_id'
belongs_to :commentable, polymorphic: true, counter_cache: true
案例表是链接:
has_many :comments, as: :commentable, dependent: :destroy
案例步骤表如下:
belongs_to :case, class_name: 'Case', foreign_key: 'case_id'
has_many :comments, as: :commentable, dependent: :destroy
通过链接形成rails casts和许多其他链接...但没有得到清晰的理解。
答案 0 :(得分:0)
评论就像
id | user_id | commentable_type | commentable_id | value
--------------------------------------------------
1 | 1 | Case | 1 | fdsfdsfsdf
2 | 1 | CaseStep | 1 | dasdfs
案例表就像
id | case_name
--------------
1 | abc
case_steps表就像
id | case_step_name | case_id
-------------------------------
1 | abc | 1
在Comment表中,commentable_type列将存储与其进行多态关联的类名,而commentable_id则是类(table)记录的id。在上述情况下,它可能是'Case'或'CaseStep'