Ruby on rails Mongoid接受\u嵌套的\u属性\u,以便在子项无效时保存父项
在创建Ruby on rails Mongoid接受\u嵌套的\u属性\u,以便在子项无效时保存父项,ruby-on-rails,ruby,ruby-on-rails-3,mongodb,mongoid,Ruby On Rails,Ruby,Ruby On Rails 3,Mongodb,Mongoid,在创建组织的过程中,我试图使用接受的嵌套属性来创建用户。我的问题是,即使嵌套的用户信息无效,也会创建组织(假设它具有有效信息) 我的模型是这样的: class Organization include Mongoid::Document attr_accessible :name, :users_attributes field :name, :type => String has_many :users, dependent: :destroy accepts_
组织
的过程中,我试图使用接受
的嵌套属性来创建用户
。我的问题是,即使嵌套的用户
信息无效,也会创建组织
(假设它具有有效信息)
我的模型是这样的:
class Organization
include Mongoid::Document
attr_accessible :name, :users_attributes
field :name, :type => String
has_many :users, dependent: :destroy
accepts_nested_attributes_for :users, :limit => 1
validates_presence_of :name
end
class User
include Mongoid::Document
authenticates_with_sorcery!
attr_accessible :email, :password, :password_confirmation
field :email, type: String
validates_confirmation_of :password, :if => :password
validates_presence_of :password, :on => :create
validates :password, :length => { :minimum => 6 }
validates_presence_of :password_confirmation, :if => :password
validates_presence_of :email
validates_uniqueness_of :email
belongs_to :organization
end
所以基本上,我传入了一个有效的组织和一个无效的用户。最终的结果是,尽管用户信息无效,组织还是正确地创建了。想法?事实证明,这里的问题是我没有在我的
上指定:autosave=>true
与许多用户的关联。最终的结果是,它将创建组织,并且从不尝试保存关联的用户,因此不知道它是无效的。当我将:autosave
标志设置为true且用户无效时,不会保存组织