我想弄清楚如何通过我的模型上的方法对返回整数进行排序。搜索按返回整数的模型方法排序
class Business < ActiveRecord::Base
has_many :questions
def questions_count
questions.count
end
end
我希望能够通过我的表中的questions_count
方法使用ransack进行排序。
我试过<th><%= sort_link @q, :questions_count %></th>
没有运气。
这甚至可能吗?我怎样才能实现它?
虽然没有使用搜索引擎,但是... – Deekor
@collection是您的搜索结果。类似于Model.search(params).sort_by {| item | item.method_name} –
但是每次都会排序,我只希望它在排序链接被点击时排序。 – Deekor