MongoMapper + Rails 协会不起作用
我想要一个与两个用户关联的 ECardMatch,并且能够通过这样的关联创建 @user.ecard_matches.new
等...
我能够执行如下操作
user = User.new
user.ecard_matches
:返回一个 []
空数组
但我不能这样做
em = EcardMatch.new
user.ecard_matches << em
错误:
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
或 user.ecard_matches.new
错误:
NoMethodError: undefined method `new' for []:Array
模型中的关联可能有问题。我是 Rails 和 mongodb 的新手,所以也许这种用法是不好的做法...
用户类:
class User
include MongoMapper::Document
attr_accessor :password
key :name, String
key :perika, Integer
key :digest_password, String
many :ecard_matches
end
EcardMatch 类:
class EcardMatch
include MongoMapper::Document
key :wager, Integer
key :turn, Integer
key :first_user_score, Integer
key :second_user_score, Integer
belongs_to :first_user, class_name: "User"
belongs_to :second_user, class_name: "User"
end
I want to have an ECardMatch that is associated to two Users, and to be able to create through associations like this @user.ecard_matches.new
etc...
I am able to do something like this:
user = User.new
user.ecard_matches
This returns an []
empty array
But I can't do
em = EcardMatch.new
user.ecard_matches << em
Error:
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
oruser.ecard_matches.new
Error:
NoMethodError: undefined method `new' for []:Array
Maybe something is wrong with my associations in the model. I am new to rails and mongodb so maybe this kind of usage is bad practice...
User class:
class User
include MongoMapper::Document
attr_accessor :password
key :name, String
key :perika, Integer
key :digest_password, String
many :ecard_matches
end
EcardMatch class:
class EcardMatch
include MongoMapper::Document
key :wager, Integer
key :turn, Integer
key :first_user_score, Integer
key :second_user_score, Integer
belongs_to :first_user, class_name: "User"
belongs_to :second_user, class_name: "User"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个例子有帮助吗?
Could this example be helpful ?