Mongodb,儿童模型验证?

时间:2012-07-06 05:13:21

标签: ruby-on-rails mongoid

我有个人资料模型,它有embeds_one kids_type和parent_type。正如您在下面的代码中看到的那样。

我想验证kids_type子模型。使用validates_associated之后,它对我来说很好。但问题是,它验证了kids_type模型[#,@ message = ** {:kids_type => [“无效”]}> **],而不是我期待的字段明智的错误,因为我需要显示内联错误...

class Profile
  include Mongoid::Document
  validates_associated :kids_type  
  # PROFILE TYPE KIDS & PARENT
  embeds_one :kids_type, :class_name => "ProfileKidsType"
  embeds_one :parent_type, :class_name => "ProfileParentType"
end

class ProfileType
  include Mongoid::Document

  #COMMON FILES FOR KIDS AND PARENT
  field :fname, :type => String
  field :lname, :type => String

  validates_presence_of :fname, :message => "ERROR: First name is required"
  validates_presence_of :lname, :message => "ERROR: Last name is required"
end

class ProfileParentType < ProfileType
  include Mongoid::Document

  field :email, :type => String  
  embedded_in :profile, :inverse_of => :parent_type
end

class ProfileKidsType < ProfileType
  include Mongoid::Document

  field :nickname, :type => String
  embedded_in :profile, :inverse_of => :kids_type
end

任何建议都将不胜感激,提前致谢。

1 个答案:

答案 0 :(得分:1)

在这里尝试这个@profile是Profile的实例,它会为孩子类型提供所有错误字段

@profile.kids_type.errors
相关问题