作者deduce (http://punk.tw/ )
看板Ruby
標題Re: [問題] count的使用
時間Sun Oct 14 00:51:10 2007
一般應該是建議寫成
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
1F:推 LyinZ:嗯 用 case Record.count(...) when 也不錯 ^^ 10/14 03:31
2F:推 godfat:喔對,之前那個可能會有 sql injection 潛在危險 10/14 07:36