Created
March 10, 2011 00:40
-
-
Save patmaddox/863330 to your computer and use it in GitHub Desktop.
I suck at mongoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
references_one :best_friend, :class_name => "Person" | |
references_one :worst_enemy, :class_name => "Person" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
include Mongoid::Document | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person = Person.create! | |
user = User.create! :best_friend => person | |
user.best_friend # => #<Person _id: 4d781dbd568ab57112000002, _type: nil, _id: BSON::ObjectId('4d781dbd568ab57112000002')> | |
user.reload | |
user.best_friend # => nil |
they're being created, just not being linked. Guess I need to spend more time reading the mongoid docs to figure out references
Your Person model is missing the inverse reference.
class Person
include Mongoid::Document
referenced_in :user
end
I'm not sure how the custom naming will fit in, though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Double-check that the record hasn't been created by going through the mongo shell. MongoMapper tends to not behave well in the console, Mongoid might be the same. Other than that, it looks right.