get child.parent.child.all in child model in rails

Viewed 13

I have 2 models parent child. I need all child of parent in child model when child has where condition.


class Parent
  has_many :children
end
class Child
  belongs_to :parent
end

I am getting data in child model like

id: 1
parent_id: 1
date: 2022-09-22

id: 2
parent_id: 1
date: 2022-09-23

id: 3
parent_id: 2
date: 2022-09-24

from Child.where(date: from_date..to_date) and this condition is coming from controller. Now I need one more field here child.parent.child.all in every record. This field is showing wrong data. it is showing data only within selected date range.
i.e. from above example from_date: 2022-09-22 and to_date: 2022-09-24 and parent_id: 2 have 4 child but in given date range it is showing only 1 because only 1 child data is falling in the given date range. but i need 4.

Thanks

0 Answers
Related