subtract from symbols in ruby -
i find first valid image among list in ruby. here code:
if(params[:id]) @image = image.find_by_id(params[:id]) while @image.nil? :id-- ? @image = image.find_by_id(params[:id]) end
in block, how can keep decreasing id # until valid image found? :/
thanks!
you can't subtract symbol. symbol not number.
what seem want decrease value of params[:id]
, of course possible (after converting id string integer) doing params[:id] = params[:id].to_i - 1
or
id = params[:id].to_i while @image.nil? @image = image.find_by_id(id) id -= 1 end
the latter better first because not mutate params
(which there no reason do).
however should not either of those, because can achieve much less hassle, letting db work:
image.find(:first, :order => "id desc", :conditions => ["id <= ?", params[:id]])
ps: ruby doesn't have --
operator, have use -= 1
decrement number.
Comments
Post a Comment