다음과 같은 일대 다 연관이 있습니다. 문서에는 많은 섹션이 있고 섹션에는 많은 항목이 있습니다.컨트롤러 동작에서 레일스 자동 저장 연결
-
Parameters: {"commit"=>"Submit Document", "authenticity_token"=>"4nx2B0pJkvavDmkEQ305ABHy+h5R4bZTrmHUv1setnc=", "id"=>"10184", "document"=>{"section"=>{"10254"=>{"seqnum"=>"3", "item"=>{"10259"=>{"comments"=>"tada"}}}}, "comment"=>"blah"}}
나는이 다음 업데이트 방법 ...
# PUT /documents/1
# PUT /documents/1.xml
def update
@document = Document.find(params[:id])
# This is header comment
@document.comment = params[:document][:comment]
params[:document][:section].each do |k,v|
document_section = @document.document_sections.find_by_id(k)
if document_section
v[:item].each do |key, value|
document_item = document_section.feedback_items.find_by_id(key)
if document_item
# This is item comments
document_item.comments = value[:comments]
end
end
end
end
@document.save
end
나는 단지 문서 헤더 주석을 업데이트 문서를 저장
: 여기class Document < ActiveRecord::Base
has_many :document_sections, :dependent => :destroy, :autosave => true
has_many :document_items, :through => :document_sections
end
class DocumentSection < ActiveRecord::Base
belongs_to :document
has_many :document_items, :dependent => :destroy, :autosave => true
end
class DocumentItem < ActiveRecord::Base
belongs_to :document_section
end
는 PARAMS 해시입니다. document_item 주석은 저장하지 않습니다. 자동 저장 옵션도 연관을 업데이트하지 않아야합니다. 로그에서
은 다음 DML은 등록 :
UPDATE documents SET updated_at = TO_DATE('2010-03-09 08:35:59','YYYY-MM-DD HH24:MI:SS'), comment = 'blah' WHERE id = 10184
가 어떻게 문서를 저장하여 연결을 저장 않습니다.
괜찮 으면 게시물의 서식을 정리하십시오. 나는 그것의 절반을 잘못 읽었습니다. 코드 비트를 강조 표시 한 다음 코드 단추를 클릭하여 올바르게 들여 쓰기를 수행합니다 (4 공백). 자신의 예제에서 각 "파일"에 대해 별도의 코드 블록을 만드는 것이 가장 좋습니다. – dpb
@ dpb 동의. 레이아웃을 약간 수정했지만 @ ash34는 구문 강조를 사용하여 개선하려고 시도합니다. 질문은 거의 읽을 수 없다. – marcgg