2014-02-23 3 views
0

제품을 편집하고 제품 모델에 포함 된 '구성 요소'모델의 행을 삭제할 때 유효성 검사에 실패하면 삭제 된 문서가 다시 편집 페이지에 나타납니다.유효성 검사에 실패한 경우 포함 문서가 제거되지 않음

업데이트 방법 :

# Put /products/:id 
def update 
    @product=Product.find(params[:id]) 

    if(@product.update_attributes(params[:product])) 
     logger.info @product.attribute.components.inspect 
     redirect_to product_path , notice:'update complete' 
    else 
     logger.info @product.attribute.components.inspect 
     render_by_type_id(@product.product_type._id,false,mode='edit') 

    end 
end 

모델 :

class Product 
    include Mongoid::Document 
    include ScopedSearch::Model 
    embeds_one :attribute ,class_name:"ProductAttribute" 
    accepts_nested_attributes_for :attribute, :product_items ,:lab_test ,allow_destroy: true 
end 



class ProductAttribute 
    include Mongoid::Document 
    embedded_in :product 
    embeds_many :components , inverse_of: nil 
    accepts_nested_attributes_for :components, :allow_destroy => true 
end 

포함 된 모델 :

class Component 
    include Mongoid::Document 

    embedded_in :product_attribute 
end 
+0

"_destroy"열의 데이터베이스 마이그레이션을 생성했는지 확인 했습니까? –

답변

0

내가 정확하게 문제를 이해하고 있는지 확실하지 않습니다,하지만 통해 파괴 점에 유의하시기 바랍니다 중첩 된 특성 is executed on save :

이 아니며 부모가 저장 될 때까지이 파괴 될 수 있습니다.

따라서 Product.create(params[:product])이 작동하면 시도해야합니다. 그러나 이것이 예상대로 작동하는지 그리고 이것이 정말로 당신이 원하는 것인지 확실하지 않습니다. 중첩 된 속성 소멸의 일반적인 사용 예는 기존 객체의 연관을 제거하는 것입니다. 한 파라메터 집합 내에서 객체 생성과 파괴를 결합하는 것이 이상하게 보입니다. 그래서 이것이 당신의 대답을하지 못하면 당신이 원하는 것을 설명하십시오.

+0

질문을 수정하고 업데이트 코드를 추가합니다. – Hlex