我有两个模型:
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为什么会发生这个错误,以及如何修复它?谢谢。
发布于 2016-09-06 16:34:03
试试这个:
class Annotation
include Mongoid::Document
belongs_to :event, class_name: 'Event::Event'
...
end缺省情况下,belongs_to关联假定关联对象的类型为Event,但Event是一个模块。这里的类名应该是Event::Event。因此,这需要在关系中指定。
如果有帮助,请告诉我。
https://stackoverflow.com/questions/39286342
复制相似问题