作者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