我有以下DB结构如何添加条件has_many关联
任务
id name parent_id
1 Abc nil
2 Pqr 1
评论
id task_id body
1 1 This is sample comment
2 1 This is another sample comment
task.rb
has_many :comments
comment.rb
belongs_to :task
我的要求是有一个协会,这样的父母以及儿童我应该得到父母的意见,即上述两个任务我应该得到['这是样品评论','这是另一个样本评论']作为小孩任务不会有任何评论。
我想是这样,但之后它不工作
task.rb
has_many :comments, -> (o) { where(comments: {task_id: [o.id, o.parent_id]}) }
_but不WORK_ - 有什么错误? – 31piy