I have a Location
model that can have many sublocations, or one parent location.
Let's say location-A is a parent location, has location-B and location-C as sublocations. But location-B also has a sublocation location-D.
How can I get all sublocations of the parent location-A, including location-D?
My model:
has_many :sub_locations, class_name: "Location", foreign_key: "parent_location_id", inverse_of: :parent_location
belongs_to :parent_location, optional: true, class_name: "Location", foreign_key: "parent_location_id", inverse_of: :sub_locations
Currently I fetch them like this:
def all_sub_location_ids
[id] + sub_locations.map(&:all_sub_location_ids).flatten
end
But I need an efficient way. Because it throws stack level too deep
Edit: I ended up using .reload
on sublocations and keep the existing method as is. This way it worked.