谢谢你的时间!
代码很简单(没有导轨使用mongoid):
require 'mongoid' # version 6.0.2
Mongoid.load!('mongoid.yml', :development)
class Office
include Mongoid::Document
embeds_one :owner
embeds_many :addresses
end
class Owner
include Mongoid::Document
end
class Address
include Mongoid::Document
end
我可以成功拨打office.addresses.build
。
office = Office.new
office.addresses.build
office.save
但是当我拨打office.owner.build
时,会弹出错误
embed_one.rb:23:in `<main>': undefined method `build' for nil:NilClass (NoMethodError)
它应该以这种方式工作,对吧?哪里错了。
puts office.owner.class # NilClass
答案 0 :(得分:1)
从睡梦中恢复过来......
我使用puts office.methods
列出office
可以调用的所有方法。
# Here's all the methods has *owner* in it
owner=
owner?
has_owner?
build_owner
create_owner
owner
office.build_owner
正是我正在寻找的!