我有两个模型:
class Annotation
include Mongoid::Document
belongs_to :event
field :desc, type: String
end
class Event::Event
include Mongoid::Document
has_many :annotations
end
然后我在rails控制台创建了两个对象,输入:
a = Annotation.new
e = Event::Event.new
现在一切都很好,但当我做
a.event = e
我得到以下错误:
NoMethodError: undefined method `relations' for Event:Module
为什么这个错误发生和如何修复它?谢谢。
试试这个:
class Annotation
include Mongoid::Document
belongs_to :event, class_name: 'Event::Event'
...
end
belongs_to
关联默认假定关联对象为Event
类型,但Event
是一个模块。这里的类名应该是Event::Event
。因此,这需要在关系中指定
如果有帮助请告诉我。