
Using has_many :through for nested relations in Rails
has_many :through is a useful association type of Rails. It’s mostly popular and often used as a join model for many-to-many relations. However, has_many :through is more than a simple join model, because it conducts INNER JOIN(s) on related models. We can also take the advantage of this behaviour on nested has_many relations. Lets imagine a scenario where we have a nested has_many structure as follows: Country (has_many :regions) -> Region (has_many :cities) -> City (has_many :districts) -> District Models and tables of the structure: ...