2012-01-12 51 views
1

我想在创建Organization期间使用accepts_nested_attributes_for创建User。我的问题是创建Organization(假定它有有效的信息),甚至当嵌套User信息无效。Mongoid acceptable_nested_attributes_for当孩子无效时保存父母

我的模式是这样的:

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 

所以基本上,我通过一个有效的组织和无效用户。最终的结果是,尽管用户信息无效,组织仍可以正确创建。思考?

回答

0

事实证明,这里的问题是,我是不是我的has_many :users协会指定:autosave => true。最终的结果是,它会创建组织,从来没有尝试保存相关的用户,因此不知道它是无效的。当我有:autosave标志设置为true,并且用户是无效的,该组织不会被保存。

相关问题