次のセットアップがあります
Class Country
include Mongoid::Document
field :name
field :code
has_many :locations
end
Class Location
include Mongoid::Document
field :country
field :region
field :city
has_many :locations
embedded_in :company
end
Class Company
include Mongoid::Document
field :name
embeds_one :location
accepts_nested_attributes_for :location
end
国モデルには、すべての国がシードされています。
国は、ネストされたフォームを介して Location モデルに 2 文字のショートコードで保存されます。たとえば、「米国」。ビューで @company.location.country.name を呼び出して「米国」を取得したいのですが、エラーが発生しています
undefined method `name' for nil:NilClass
どうすればこれを行うことができますか?最善の方法は何ですか?私はMongoDBを初めて使用するので、これがばかげた質問であれば申し訳ありません