2012-01-12 5 views
1

の作成中にUserを作成するのにを使用しようとしています。私の問題は、入れ子になったUser情報が無効であっても、Organizationが(有効な情報を持っていると仮定して)作成されていることです。Mongoidは子が無効な場合に親を保存することを承諾します

私のモデルは次のようになります。

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に設定されていて、ユーザーが無効な場合、組織は保存されません。

関連する問題