如何使 mongo 映射器需要一个 own_to ?
我正在使用 Mongo Mapper,我试图弄清楚如何使文档成为必需的。例如,我想做这样的事情:
class Question
include MongoMapper::Document
many :answers
end
class Answer
include MongoMapper::Document
belongs_to :question, :required => true
end
但是当我这样做时,并不强制答案有一个问题:
> Answer.new.save
=> true
I am using Mongo Mapper and I'm trying to figure out how you make a document required. For example, I want to do something like this:
class Question
include MongoMapper::Document
many :answers
end
class Answer
include MongoMapper::Document
belongs_to :question, :required => true
end
But when I do, it's doesn't enforce that the answer have a question:
> Answer.new.save
=> true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoMapper 包含 ActiveModel::Validations,因此您可以像活动记录一样使用验证。以下内容应该有效:
您可以在此处查看 Rails 文档以获取有关这些验证的更多信息: http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates
MongoMapper includes ActiveModel::Validations so you can use validations just like active record. The following should work:
You can check the rails docs for more info on those validations here: http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates
一些回调怎么样?
What about some callbacks?