0

刚刚从 mongo_mapper 切换到 mongoid,我发现一切都更加明确。通过关系,我定义了两个对象之间的多态关系。

两个对象 apples & fruit_bowl 被定义为:

class Apples
include Mongoid::Document

field colour, type: String

belongs_to :fruits, :polymorphic=>true
end

和:

class FruitBowl
include Mongoid::Document

field size, type: Integer

has_many :apples, as: :fruits, validate: false 
end

当我分别创建一个fruit_bowl 和一个苹果,然后尝试将苹果放入碗中时,出现错误... undefined method __ bson_dump __

我使用的代码是:

apple = Apple.create(colour: 'Red')
fruit_bowl = FruitBowl.create(size: 5)
fruit_bowl << apple
fruit_bowl.save #Errors here

我究竟做错了什么?

4

1 回答 1

3

试试fruit_bowl.apples <<苹果

于 2013-02-26T17:55:38.633 回答