Mongoid 使用 has_one 将一个对象与同一类的两个不同对象相关联

发布于 2024-11-01 02:40:19 字数 1519 浏览 0 评论 0原文

我已经看到了 1:N 这类问题的解决方案,但他们似乎没有阅读到 1:1,这是使用 MongoDB 1.8、Mongoid 2.0.0.rc.8、Rails 3.0.5

class Coach  
  include Mongoid::Document 
  field :name, :type => String 
  belongs_to :coached, :class_name => Team, :inverse_of => :coach, :foreign_key => "coach_id" 
  belongs_to :assisted, :class_name => Team, :inverse_of => :assist, :foreign_key => "assist_id" 
end 


class Team 
  include Mongoid::Document 
  field :name, :type => String 
  has_one :coach, :class_name => Coach, :inverse_of => :coached 
  has_one :assist, :class_name => Coach, :inverse_of => :assisted 
end 

然后我开始和 Rails控制台会话和:

irb(main):001:0> c = Coach.new(:name => "Tom")  
=> #<Coach _id: da18348d298ca47ad000001, _type: nil, _id: BSON::ObjectId('4da18348d298ca47ad000001'), name: "Tom", coach_id: nil, assist_id: nil> 

irb(main):002:0> a = Coach.new(:name => "Dick") 
=> #<Coach _id: 4da18352d298ca47ad000002, _type: nil, _id: BSON::ObjectId('4da18352d298ca47ad000002'), name: "Dick", coach_id: nil, assist_id: nil> 

irb(main):003:0> t = Team.new(:name => "Allstars") 
=> #<Team _id: 4da18362d298ca47ad000003, _type: nil, _id: BSON::ObjectId('4da18362d298ca47ad000003'), name: "Allstars"> 

irb(main):005:0> t.coach = c 
NoMethodError: undefined method `constantize' for Coach:Class 

irb(main):005:0> c.coached = t 
NoMethodError: undefined method `constantize' for Team:Class 

任何建议将不胜感激!

I have see solutions to this sort of issue with 1:N, but they dont seem to read across to 1:1, this is using MongoDB 1.8, Mongoid 2.0.0.rc.8, Rails 3.0.5

class Coach  
  include Mongoid::Document 
  field :name, :type => String 
  belongs_to :coached, :class_name => Team, :inverse_of => :coach, :foreign_key => "coach_id" 
  belongs_to :assisted, :class_name => Team, :inverse_of => :assist, :foreign_key => "assist_id" 
end 


class Team 
  include Mongoid::Document 
  field :name, :type => String 
  has_one :coach, :class_name => Coach, :inverse_of => :coached 
  has_one :assist, :class_name => Coach, :inverse_of => :assisted 
end 

Then I start and Rails Console session and:

irb(main):001:0> c = Coach.new(:name => "Tom")  
=> #<Coach _id: da18348d298ca47ad000001, _type: nil, _id: BSON::ObjectId('4da18348d298ca47ad000001'), name: "Tom", coach_id: nil, assist_id: nil> 

irb(main):002:0> a = Coach.new(:name => "Dick") 
=> #<Coach _id: 4da18352d298ca47ad000002, _type: nil, _id: BSON::ObjectId('4da18352d298ca47ad000002'), name: "Dick", coach_id: nil, assist_id: nil> 

irb(main):003:0> t = Team.new(:name => "Allstars") 
=> #<Team _id: 4da18362d298ca47ad000003, _type: nil, _id: BSON::ObjectId('4da18362d298ca47ad000003'), name: "Allstars"> 

irb(main):005:0> t.coach = c 
NoMethodError: undefined method `constantize' for Coach:Class 

irb(main):005:0> c.coached = t 
NoMethodError: undefined method `constantize' for Team:Class 

any advice would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-11-08 02:40:19

您在定义 Coach 时引用了类 Team,但该类尚不存在。
您有两个选择:

  • class_name 声明为字符串而不是常量,例如 :class_name => 'Team' (首选,请参阅 gist
  • 完全删除 :class_name => Team 选项,让 Mongoid 找出参与关联的正确类。 有一个警告:您需要确保类 Team 在类 Coach 之前声明(加载源代码的顺序)现在很重要,所以这个解决方案并不理想)

You're referencing the class Team when defining Coach, but the class does not yet exist.
You have two options:

  • Declare the class_name as a String instead of the constant e.g. :class_name => 'Team' (preferred, see gist)
  • Completely remove :class_name => Team option and let Mongoid figure out the correct classes participating in the association. There is one caveat: You will need to make sure that the class Team is declared before the class Coach (the order of loading your source codes now matters, so this solution is not ideal)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文