如何进行单个查询以通过 table in ruby on rails 中的联接检索与模型相关的对象
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 之类的东西,并通过其相关对象拥有属于一个国家/地区的所有城市return。感谢任何反馈,谢谢!
尝试:
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
我有相关型号如下:
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 之类的东西,并通过其相关对象拥有属于一个国家/地区的所有城市return。感谢任何反馈,谢谢!
尝试:
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