Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created March 10, 2011 00:40
Show Gist options
  • Save patmaddox/863330 to your computer and use it in GitHub Desktop.
Save patmaddox/863330 to your computer and use it in GitHub Desktop.
I suck at mongoid
class User
include Mongoid::Document
references_one :best_friend, :class_name => "Person"
references_one :worst_enemy, :class_name => "Person"
end
class Person
include Mongoid::Document
end
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
@batasrki
Copy link

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.

@patmaddox
Copy link
Author

they're being created, just not being linked. Guess I need to spend more time reading the mongoid docs to figure out references

@batasrki
Copy link

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