我得到了這段代碼,當我嘗試使用第二種更優雅的方法時,我得到了「缺少方法」。我認爲文檔在:http://guides.rubyonrails.org/active_record_querying.html是相當無益或不完整的。有任何想法嗎?在ActiveRecord上Ruby on Rails chaning方法不起作用?
順便說一句,隨時幫忙寫一個更加「oneliner」優雅的辦法處理這一:)
def index
@sortby = params[:sort_by]
# THIS WORKS
#if @sortby == nil
# @movies = Movie.all
#else
# @movies = Movie.order(@sortby)
#end
# THIS DOESNT. WHY? I THOUGHT METHOD CHAINING AND LAZYLOAD WOULD WORK.
@movies = Movie.all
@movies = @movies.order(@sortby) unless @sortby == nil
末
你的代碼是開放的SQL注入攻擊:http://stackoverflow.com/questions/7771103/rails-3-activerecord-order-what-is-the-proper-sql-injection-work-around – house9 2013-02-10 16:12:08