객체를 파괴하는 대신 암살 제거를 제거 할 때 : allow_destroy => true
-
18-09-2019 - |
문제
새로운 것을 사용할 때 accepts_nested_attributes_for
ActiveRecord에서는 옵션을 사용할 수 있습니다. :allow_destroy => true
. 이 옵션이 설정되면 중첩 된 속성이 포함 된 해시가 {"_delete"=>"1", "id"=>"..."}
전달되었습니다 update_attributes
중첩 된 물체를 삭제합니다.
간단한 설정 :
class Forum < ActiveRecord::Base
has_many :users
accepts_nested_attributes_for :users, :allow_destroy => true
end
class User < ActiveRecord::Base
belongs_to :forum
end
Forum.first.update_attributes("users_attributes"=>{"0"=>{"_delete"=>"1", "id"=>"42"}})
의문: 어떻게하나요? - 중첩 된 물체를 삭제하는 대신 "_delete" => "1"
- 단지 협회를 제거 하시겠습니까? (위의 경우, 사용자의 포럼을 nil로 설정합니다)
보너스 질문 : 연관성을 제거 할 때 중첩 된 객체의 속성을 변경하려면 어떻게해야합니까? (예 : 상태 또는 타임 스탬프 설정을 좋아합니다)
해결책
사용자에게 사용을 삭제하도록 요청하는 대신 "_delete" => '1'
, Nested_attributes를 사용하여 업데이트 할 수 없습니까? :
Forum.first.update_attributes("users_attributes"=> {
"0" => {
"id" => "42",
"forum_id" => "",
"state" => 'removed'
}
})
제휴하지 않습니다 StackOverflow