Reputation: 666
I have an app with many users, that can create many travels, and they can invite friends to participate to the travel.
Here are the models :
class Travel
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user
embeds_many :participants
accepts_nested_attributes_for :participants
end
class Participant
include Mongoid::Document
include Mongoid::Timestamps
# relations
embedded_in :travel, :inverse_of => :participants
end
When I look my DB I have this result for participants :
It is embedded in travel. Right now, I want to access user, thanks to the user_id that you see in the DB, on the travel page, but I don't know what request I have to write to implement that. I want to access users which are participants.
Does I have to use .where()
like : User.where(:id.in => ...)
or .find_by()
.. ?
Upvotes: 0
Views: 112
Reputation: 6121
Since Travel
and User
are associated (belongs_to
), you can call directly..
Travel.last.user
Or
User.where(id: Travel.participants.pluck(:user_id))
Upvotes: 1