Skip to content

Instantly share code, notes, and snippets.

@chanks
Created January 10, 2011 08:22
Show Gist options
  • Save chanks/772533 to your computer and use it in GitHub Desktop.
Save chanks/772533 to your computer and use it in GitHub Desktop.
class Quiz
include Mongoid::Document
embeds_many :topics
# fields...
end
class Topic
include Mongoid::Document
embedded_in :quiz
end
quiz = Fabricate :quiz
=> #<Quiz _id: 4d2ac0cf0de0691cb5000666, created_at: 2011-01-10 08:18:23 UTC, updated_at: 2011-01-10 08:18:23 UTC, user_id: BSON::ObjectId('4d2ac0cf0de0691cb5000667'), time: 0, number: 1, built_at: nil, completed_at: nil, graded_at: nil>
quiz.attributes
=> {"time"=>0, "_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000666'), "user_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000667'), "created_at"=>2011-01-10 08:18:23 UTC, "number"=>1, "updated_at"=>2011-01-10 08:18:23 UTC, "topics"=>[{"_id"=>1}, {"_id"=>3}]}
quiz.topics
=> [#<Topic _id: 1, score: nil, given: nil, correct: nil>, #<Topic _id: 3, score: nil, given: nil, correct: nil>]
quiz.topics = [Topic.new(:_id => 2)]
=> [#<Topic _id: 2, score: nil, given: nil, correct: nil>]
quiz.topics
=> [#<Topic _id: 2, score: nil, given: nil, correct: nil>]
quiz.attributes
=> {"time"=>0, "_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000666'), "user_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000667'), "created_at"=>2011-01-10 08:18:23 UTC, "number"=>1, "updated_at"=>2011-01-10 08:18:23 UTC, "topics"=>[{"_id"=>1}, {"_id"=>3}]}
quiz.save
=> true
quiz.attributes
=> {"time"=>0, "_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000666'), "user_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000667'), "created_at"=>2011-01-10 08:18:23 UTC, "number"=>1, "updated_at"=>2011-01-10 08:19:42 UTC, "topics"=>[{"_id"=>1}, {"_id"=>3}]}
quiz.reload
=> #<Quiz _id: 4d2ac0cf0de0691cb5000666, created_at: 2011-01-10 08:18:23 UTC, updated_at: 2011-01-10 08:19:42 UTC, user_id: BSON::ObjectId('4d2ac0cf0de0691cb5000667'), time: 0, number: 1, built_at: nil, completed_at: nil, graded_at: nil>
quiz.attributes
=> {"_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000666'), "created_at"=>2011-01-10 08:18:23 UTC, "number"=>1, "time"=>0, "topics"=>[{"_id"=>2}], "updated_at"=>2011-01-10 08:19:42 UTC, "user_id"=>BSON::ObjectId('4d2ac0cf0de0691cb5000667')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment