【发布时间】:2012-07-02 23:31:39
【问题描述】:
我有以下设置
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
Countries 模型包含所有国家/地区。
国家/地区通过嵌套形式存储在 Location 模型中的 2 个字母短代码。例如“美国”。 现在我想在视图中调用@company.location.country.name 以获取“美国”,但是我收到一个错误
undefined method `name' for nil:NilClass
我该怎么做呢? 最好的方法是什么? 我是 MongoDB 新手,所以如果这是一个愚蠢的问题,我深表歉意
【问题讨论】:
-
你为什么没有
has_and_bleongs_to_many关系?
标签: ruby-on-rails ruby-on-rails-3 mongodb ruby-on-rails-3.1 mongoid