ABrowne
ABrowne

Reputation: 1604

mongoid - can someone explain relationships to me

Having just switched from mongo_mapper to mongoid, I am finding that everything is more explicit. With relationships I have defined a polymorphic relationship between two objects.

The two objects apples & fruit_bowl have been defined as:

class Apples
include Mongoid::Document

field colour, type: String

belongs_to :fruits, :polymorphic=>true
end

and:

class FruitBowl
include Mongoid::Document

field size, type: Integer

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

When I create a fruit_bowl & an apple seperately and then attempt to put the apple in the bowl I get an error... undefined method __ bson_dump __

The code I use is:

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

What am I doing wrong?

Upvotes: 0

Views: 106

Answers (1)

Chen Kinnrot
Chen Kinnrot

Reputation: 21025

try fruit_bowl.apples << apple

Upvotes: 3

Related Questions