【发布时间】:2011-05-31 16:54:10
【问题描述】:
我正在使用 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。
【问题讨论】:
-
我最好的选择是向商店添加一个“current_inventory_records_count”数据库列,我只是确保使用 before_save's 总是正确的?
标签: ruby-on-rails activerecord meta-search