[問題] 如何以DRY的方式寫code?

看板Ruby作者 (甚麼時候才等到妳)時間15年前 (2009/10/07 11:25), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串1/4 (看更多)
環境: Ruby 1.8.7 Rails 2.3.4 狀況: 我寫的是一個企業管理程式,在每開一個新的作業流程都會先取得一個新的編號 (不是預設的id)。例如Quotation,我在Quotations Helper里是這樣寫: module QuotationsHelper def new_doc_no Quotation.find(:first, :order => 'doc_no DESC').doc_no + 1; end end 至于其他的作業流程是否也要寫類似的helper function,還是有更DRY的方式? 謝謝指教 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.101.213.59

10/07 11:29, , 1F
這樣寫在有很多人同時使用時會有問題。doc_no有可能重覆
10/07 11:29, 1F

10/07 11:47, , 2F
在正式寫入database時會用transaction檢查一遍,再寫入
10/07 11:47, 2F

10/07 11:48, , 3F
然後會用一個alert告知使用者doc_no已經修改了
10/07 11:48, 3F
文章代碼(AID): #1Ap0emd9 (Ruby)
文章代碼(AID): #1Ap0emd9 (Ruby)