0
我想实现评论回复功能到我的项目,但我不是很确定我使用的方法。我的基本想法是将所有的评论保留在一张表中,同时有另一个表comments_replies这将有父母的评论(评论)和评论(回复)。现在我有这样的事情是comments_replies迁移:在同一张表中有一张表有两个外键rails
模型comments_reply.rbbelongs_to :comment, class_name: 'Comment'
,并在模型comment.rb
create_table :comments_replies do |t|
t.integer :parent_comment_id, index: true, foreign_key_column_for: :comments, null: false
t.integer :reply_comment_id, index: true, foreign_key_column_for: :comments, null: false
t.timestamps null: false
end
和
has_many :comments_replies, foreign_key: :parent_comment_id, foreign_key: :reply_comment_id
对于第二部分,因为即时尝试使用RSPEC用于测试目的,在模型comments_reply_spec.rb我有:
require 'rails_helper'
RSpec.describe CommentsReply, type: :model do
let(:comments_reply) { create(:comments_reply) }
subject { comments_reply }
it { is_expected.to respond_to(:parent_comment_id) }
it { is_expected.to respond_to(:reply_comment_id) }
end
,但我不知道如何正确地测试这种情况下,因此任何建议,将不胜感激
我错过了建议的方法,我相信它会更容易和更加有用,但是应该如何我在迁移和模型中指定parent_id字段,是否可以简单地在迁移中写入: t.integer:parent_id,index:true,foreign_key_column_for :::comments,null:false 评论模型: belongs_to:comment,foreign_key::parent_id,class_name:'Comment' 还有一件事,在评论模型中,我需要写一些类似于: has_many:comments,foreign_key :: parent_id – Hatik