0
我有許多用戶的應用程序,可以創建許多旅行,他們可以邀請朋友參與到旅行中。Rails mongoid - 訪問user_id存在於另一個表中的用戶
下面是型號:
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
它被鑲嵌在旅行。 現在,我想訪問用戶,這要歸功於您在數據庫中看到的user_id,在旅行頁面上,但我不知道要寫入什麼請求才能實現該功能。我想訪問參與者的用戶。
是否必須使用.where()
:User.where(:id.in => ...)
或.find_by()
..?
我不知道理解,我想訪問被邀請的用戶在旅行中(在嵌入式文檔 - >參與者中),當我這樣做時:'@ travel.participant.last.user'我有這個錯誤:'未定義的方法'用戶'爲#<參與者:0x007ffdaf6e0e88>你的意思是? user_id',我想訪問所有被邀請參加旅行的參與者 –
檢查更新,我認爲參與者和用戶是相關聯的..我應該是 –
感謝buddy它運作良好,我不知道'pluck' , 這是完美的 ! –