Rails 3 和 Mongoid:似乎无法建立一对多关系
我有一个用户模型和一个跟踪器模型,
目标是让用户拥有: - 该用户创建的一个跟踪器 - 用户的一个引用跟踪器
另一方面,每个跟踪器: - 属于一个用户,即创建它的那个用户 - 有许多用户推荐人(曾经使用此跟踪器作为推荐人)
所以我按照以下方式构建了我的模型:
class User
.
.
references_one :referer_tracker, :class_name => "Tracker", :inverse_of => :referees
references_one :tracker, :class_name => "Tracker", :inverse_of => :owner
.
.
end
class Tracker
.
.
belongs_to :owner, :class_name => "User", :inverse_of => :tracker
has_many :referees, :class_name => "User", :inverse_of => :referer_tracker
.
.
end
User.tracker / Tracker.owner 关系工作得很好。 但我无法让 User.referer_tracket // Tracker.referees 工作。
使用控制台,如果执行以下操作:
trackerX.referees << userX
trackerX.save
=> false because of invalid referees
在另一端,如果我尝试执行以下操作: userX.referer_tracker = trackerX 我收到错误: NoMethodError: # 的未定义方法“条目”
我已经没有办法尝试了...我正在运行 RoR 3.03 和 Mongoid 2.0.0.rc.7
谢谢,
Alex
I have a User model and a Tracker model
the goal is to have a user having:
- one tracker that was created by this user
- one referer tracker to the user
On the other hand, each tracker:
- belong to one user, the one one who created it
- have many user referees (who ever used this tracker as a referer)
So I built my model the following way:
class User
.
.
references_one :referer_tracker, :class_name => "Tracker", :inverse_of => :referees
references_one :tracker, :class_name => "Tracker", :inverse_of => :owner
.
.
end
class Tracker
.
.
belongs_to :owner, :class_name => "User", :inverse_of => :tracker
has_many :referees, :class_name => "User", :inverse_of => :referer_tracker
.
.
end
The User.tracker / Tracker.owner relationship works just fine.
But I can't get the User.referer_tracket // Tracker.referees one to work.
With the console if do something like this:
trackerX.referees << userX
trackerX.save
=> false because of invalid referees
on the other end if I try to do:
userX.referer_tracker = trackerX I get an error:
NoMethodError: undefined method `entries' for #
I am running out of ideas to try... I am running RoR 3.03 and Mongoid 2.0.0.rc.7
Thanks,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论