我目前正在开发一个小型 Rails 3 应用程序,以帮助跟踪工作中的秘密圣诞老人。我几乎完成了并且完全难以解决最后一个问题。
我有一个Participant
mongoid文档,需要一个自加入来代表谁必须为谁买礼物。无论我做什么,我似乎都无法让它发挥作用。我的代码如下:
# app/models/participant.rb
class Participant
include Mongoid::Document
include Mongoid::Timestamps
field :first_name, :type => String
field :last_name, :type => String
field :email, :type => String
# --snip--
referenced_in :secret_santa, :class_name => "Participant", :inverse_of => :receiver
references_one :receiver, :class_name => "Participant", :inverse_of => :secret_santa
使用 rails 控制台,如果我设置任何一个属性,它永远不会反映在连接的另一侧,有时在保存和重新加载后会一起丢失。我敢肯定,答案让我眼前一亮——但经过数小时的凝视,我仍然看不到它。