Mongoid 使用 has_one 将一个对象与同一类的两个不同对象相关联
我已经看到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在定义
Coach
时引用了类Team
,但该类尚不存在。您有两个选择:
class_name
声明为字符串而不是常量,例如:class_name => 'Team'
(首选,请参阅 gist):class_name => Team
选项,让 Mongoid 找出参与关联的正确类。 有一个警告:您需要确保类Team
在类Coach
之前声明(加载源代码的顺序)现在很重要,所以这个解决方案并不理想)You're referencing the class
Team
when definingCoach
, but the class does not yet exist.You have two options:
class_name
as a String instead of the constant e.g.:class_name => 'Team'
(preferred, see gist):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 classTeam
is declared before the classCoach
(the order of loading your source codes now matters, so this solution is not ideal)