Okay I can definitely explain what's going on here. Given the following models:
class User
include Mongoid::Document
#some fields
has_one :team
end
class Team
include Mongoid::Document
#some fields
belongs_to :user
end
We can create the documents as follows:
Team.create!(user: User.create!)
This is equivalent to what you did in the OP. Now, this yields the following documents:
> db.users.find()
{ "_id" : ObjectId("62d9ad683282a400a799c7e7") }
> db.teams.find()
{ "_id" : ObjectId("62d9ad683282a400a799c7e8"), "user_id" : ObjectId("62d9ad683282a400a799c7e7") }
As you can see, the foreign key is stored on the belongs_to side of the relationship. Now, when trying to retrieve the team from the user:
User.last
# => #<User _id: 62d9adc63282a400c637b234, >
User.last.team
The take method (as of Mongoid 8, before that, the first method was used) is used on the following criteria:
#<Mongoid::Criteria
selector: {"user_id"=>BSON::ObjectId('62d9adc63282a400c637b234')}
options: {}
class: Team
embedded: false>
To put this into layman's terms, we are searching the Team collection for a document with the same value for user_id as our user's _id.
So yes, your idea is almost correct, it's more like:
Team.where(user_id: user._id).take
The difference between take and first is that take doesn't put a sort on _id, while first does. Using take is a little more efficient.