Re: [問題] count的使用

看板Ruby作者 (http://punk.tw/ )時間17年前 (2007/10/14 00:51), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
一般應該是建議寫成 Record.count(:conditions => ["seatnumber = ?", @record.seatnumber]) 可以試著養成習慣,除了可以避免不必要的麻煩之外,也比#{}看起來整齊一點 此外...如果是我...會這樣寫 count = Record.count(:conditions => ["seatnumber = ?", @record.seatnumber]) 然後 if 裡面用 count...這樣應該會少一次query吧~ : @record = Record.new(params[:record]) : if Record.count(:conditions => "seatnumber = @record.seatnumber") == 0 : @record.punishment = "無" : elsif Record.count(:conditions => "seatnumber = @record.seatnumber") == 1 : @record.punishment = "週末輔導" : else : @record.punishment = "警告乙支" : end : if @record.save : flash[:notice] = '紀錄新增成功!' : redirect_to :action => 'list' : else : render :action => 'new' : end : end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.119.235.209

10/14 03:31, , 1F
嗯 用 case Record.count(...) when 也不錯 ^^
10/14 03:31, 1F

10/14 07:36, , 2F
喔對,之前那個可能會有 sql injection 潛在危險
10/14 07:36, 2F
文章代碼(AID): #174FU0k5 (Ruby)
討論串 (同標題文章)
本文引述了以下文章的的內容:
3
3
完整討論串 (本文為第 2 之 2 篇):
3
3
文章代碼(AID): #174FU0k5 (Ruby)