我有一個應用程序,用戶可以創建很多旅行,並且他們可以邀請他們的Facebook好友。在旅行證件中,有一個字段「參與者」是一個嵌入文件,Participant
模型嵌入在Travel
模型中。 Rails Mongoid:查詢嵌入式文檔並訪問Mongoid標準
這裏是我的模型:
class Travel
include Mongoid::Document
include Mongoid::Timestamps
# relations
belongs_to :user
# fields
field :title, type: String
field :description, type: String
field :begin_date, type: Date
field :end_date, type: Date
field :budget, type: Integer
field :go_back, type: Boolean
field :title_namespace, type: String
# friends
embeds_many :participants
accepts_nested_attributes_for :participants
end
class Participant
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :user_id, type: String
# relations
embedded_in :travel, :inverse_of => :participants
end
當我嘗試顯示,用戶已經被邀請旅遊,和這個請求:
@travel_participations = Travel.where('participants.user_id' => @user.id)
我沒有任何結果,即使如果我在byebug有這條線:
#<Mongoid::Criteria
selector: {"participants.user_id"=>BSON::ObjectId('592c8da58511989ec850921e')}
options: {}
class: Travel
embedded: false>
所以當我把這個在我的觀點:
<% unless @participations.nil? %>
<% @travel_participations.each do |travel_participation| %>
<p> <%= travel_participation.title %> </p>
<% end %>
<% end %>
我試圖與.all
,.first
,.to_a
,.as_json
,沒有結果......有些人知道問題出在哪裏?
完美的答案,很明顯,我有解釋,很多朋友的感謝!我將你的答案標記爲已接受! –
那麼,沒有解釋的答案不會很多,是嗎?字符串和ObjectIds之間的區別非常煩人。 –