Ruby on rails 3 多态关联数据存储
有人能解释一下数据是如何存储在多态关联中的吗 e、 如果我有意见,案例,案例步骤表。 注释表是链接:Ruby on rails 3 多态关联数据存储,ruby-on-rails-3,polymorphic-associations,Ruby On Rails 3,Polymorphic Associations,有人能解释一下数据是如何存储在多态关联中的吗 e、 如果我有意见,案例,案例步骤表。 注释表是链接: 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 :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 :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
Goen通过rails Cast的链接和许多其他链接。。。但是没有得到清晰的理解。评论会像
id | user_id | commentable_type | commentable_id | value
--------------------------------------------------
1 | 1 | Case | 1 | fdsfdsfsdf
2 | 1 | CaseStep | 1 | dasdfs
id | case_name
--------------
1 | abc
id | case_step_name | case_id
-------------------------------
1 | abc | 1
案例表将是这样的
id | user_id | commentable_type | commentable_id | value
--------------------------------------------------
1 | 1 | Case | 1 | fdsfdsfsdf
2 | 1 | CaseStep | 1 | dasdfs
id | case_name
--------------
1 | abc
id | case_step_name | case_id
-------------------------------
1 | abc | 1
case_步骤表如下所示
id | user_id | commentable_type | commentable_id | value
--------------------------------------------------
1 | 1 | Case | 1 | fdsfdsfsdf
2 | 1 | CaseStep | 1 | dasdfs
id | case_name
--------------
1 | abc
id | case_step_name | case_id
-------------------------------
1 | abc | 1
在Comment table中,commentable_type列将存储它与多态关联的类名,commentable_id是类(表)记录的id。在上述情况下,可能是“案例”或“案例步骤”