[英]How to make a single query to retrieve objects related to a model through a join table in ruby on rails
我有以下相关模型:
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
我希望能够调用类似@ country.cities之类的东西,并使其通过其相关对象返回属于一个国家的所有城市。 任何反馈表示赞赏,谢谢!
尝试:
Country
has_many :states
has_many :cities, through: :states
State
has_many :counties
has_many :cities, through: :counties
干得好:
Country
has_many :states
has_many :counties , through: :states
has_many :municipalities, through: :counties
has_many :cities , through: :municipalities
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.