我正在使用 meta_search 对表中的列进行排序。我的表列之一是特定模型的关联记录的计数。
基本上是这样的:
class Shop < ActiveRecord::Base
has_many :inventory_records
def current_inventory_count
inventory_records.where(:current => true).count
end
end
class InventoryRecord < ActiveRecord::Base
belongs_to :shop
#has a "current" boolean on this which I want to filter by as well
end
在我的 Shop#index 视图中,我有一个表格,列出了每个商店的 current_inventory_count。反正有没有使用 meta_search 按这个计数来订购商店?
我不能使用我的 current_inventory_count 方法,因为 meta_search 只能使用返回 ActiveRecord::Relation 类型的自定义方法。
我可以考虑这样做的唯一方法是执行一些自定义 SQL,其中包括“虚拟”列中的计数并按此列进行排序。我不确定这是否可能。
有任何想法吗?
我正在使用 Rails 3.0.3 和最新的 meta_search。