Issue
I have models related as follows:
Country
has_many :states
State
belongs_to :country
has_many :counties
County
belongs_to :State
has_many :municipalities
has_many :cities, through: :municipalities
City
has_many :municipalities
has_many :counties, through: :municipalities
Municipality
belongs_to :county
belongs_to :city
I'd like to be able to call something like @country.cities and have it return all the cities that belong to a country through its related objects. Any feedback is appreciated, thanks!
Solution
Here you go:
Country
has_many :states
has_many :counties , through: :states
has_many :municipalities, through: :counties
has_many :cities , through: :municipalities
Answered By - David Aldridge Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.