如果您访问http://ccvideofinder.heroku.com/,这是我所说的一个很好的例子。
如何在Rails中实现呢?我曾经想过使用
在模型中:
在控制器中:
如何在Rails中实现呢?我曾经想过使用
case
/when
语句,但是在IRB上摸索了一段时间后,我还是无法弄清楚。
在模型中:
class Movies < ActiveRecord::Base
validates_presence_of :title
def self.find_by_first_letter(letter)
find(:all, :conditions => ['title LIKE ?', "#{letter}%"], :order => 'title ASC')
end
end
在控制器中:
@result = Movie.find_by_first_letter(params[:letter])