User.rb模型validate_presence_of父属性mongoid
class User
include Mongoid::Document
# relationships
has_one :post
#fields
field :name, :type => String
field :last_name, :type => String
end
Post.rb模型
class Post
include Mongoid::Document
# relationships
belongs_to :user
#fields
field :title, :type => String
field :description, :type => String
#validations here
end
之前创建一个帖子我想验证用户有name
和last_name
。另外,我想表现出错误,如果用户还没有name
或last_name
这些验证都与回调的模式进行,或者必须在控制器上执行?
谢谢!
你看了'validates_associated'? – Jesper 2013-03-04 21:20:36