我有以下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