(byebug) country.states
#<ActiveRecord::Associations::CollectionProxy [#<State id: 2, name: "ブリティッシュコロンビア州", name_en: "BC", country_id: 3, created_at: "2017-06-30 08:21:05", updated_at: "2017-06-30 08:21:05">]>
こんなのでは取り出せない↓
(byebug) country.states.cities
*** NoMethodError Exception: undefined method `cities' for #<State::ActiveRecord_Associations_CollectionProxy:0x007faa63bf9fb8>
nil
(byebug) country.states.city
*** NoMethodError Exception: undefined method `city' for #<State::ActiveRecord_Associations_CollectionProxy:0x007faa63bf9fb8>
nil
(byebug) country.states.cities
*** NoMethodError Exception: undefined method `cities' for #<State::ActiveRecord_Associations_CollectionProxy:0x007faa63bf9fb8>
nil
(byebug) country.cities
*** NoMethodError Exception: undefined method `cities' for #<Country:0x007faa632b8cd8>
nil
(byebug) country.city
*** NoMethodError Exception: undefined method `city' for #<Country:0x007faa632b8cd8>
nil
(byebug) city
*** NameError Exception: undefined local variable or method `city' for #<RSpec::ExampleGroups::Post::Nested:0x007faa658ad088>
nil
(byebug) City
City(id: integer, name: string, name_en: string, state_id: integer, created_at: datetime, updated_at: datetime)
(byebug) City.all
#<ActiveRecord::Relation [#<City id: 1, name: "ワシントン", name_en: "Washington, D.C.", state_id: 2, created_at: "2017-06-30 08:21:05", updated_at: "2017-06-30 08:21:05">]>
このように.firstを使って取り出す↓
(byebug) country.states.first
#<State id: 2, name: "ブリティッシュコロンビア州", name_en: "BC", country_id: 3, created_at: "2017-06-30 08:21:05", updated_at: "2017-06-30 08:21:05">
しかし、これではダメ↓
(byebug) country.states.first.city
*** NoMethodError Exception: undefined method `city' for #<State:0x007faa6397b8e0>
has_manyなのできちんと複数形で書いて、.firstで取り出す↓
(byebug) country.states.first.cities
#<ActiveRecord::Associations::CollectionProxy [#<City id: 1, name: "ワシントン", name_en: "Washington, D.C.", state_id: 2, created_at: "2017-06-30 08:21:05", updated_at: "2017-06-30 08:21:05">]>
cityも.firstで取り出す↓
(byebug) country.states.first.cities.first
#<City id: 1, name: "ワシントン", name_en: "Washington, D.C.", state_id: 2, created_at: "2017-06-30 08:21:05", updated_at: "2017-06-30 08:21:05">