Rails meta_search gem:按关联模型的计数排序

发布于 2024-10-09 11:11:32 字数 720 浏览 1 评论 0原文

我正在使用 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。

I'm using meta_search to sort columns in a table. One of my table columns is a count of the associated records for a particular model.

Basically it's this:

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

In my Shop#index view I have a table that lists out the current_inventory_count for each Shop. Is there anyway to use meta_search to order the shops by this count?

I can't use my current_inventory_count method as meta_search can only use custom methods that return an ActiveRecord::Relation type.

The only way I can think about doing this is to do some custom SQL which includes the count in a "virtual" column and do the sorting by this column. I'm not sure if that's even possible.

Any Ideas?

I'm using Rails 3.0.3 and the latest meta_search.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2024-10-16 11:11:32

要向结果集中添加额外的列...

在 Shop.rb 中 ..

scope :add_count_col, joins(:inventory_records).where(:current=>true).select("shops.*, count(DISTINCT inventory_records.id) as numirecs").group('shops.id')

scope :sort_by_numirecs_asc, order("numirecs ASC")
scope :sort_by_numirecs_desc, order("numirecs DESC")

在shops_controller.rb 索引方法中

@search = Shop.add_count_col.search(params[:search])
#etc.

在index.html.erb 中

<%= sort_link @search, :numirecs, "Inventory Records" %>

找到 sort_by__asc 参考: http://metautonomo.us/2010/11/21/metasearch-metawhere-and-rails-3-0-3/

To add extra columns to a result set...

In Shop.rb ..

scope :add_count_col, joins(:inventory_records).where(:current=>true).select("shops.*, count(DISTINCT inventory_records.id) as numirecs").group('shops.id')

scope :sort_by_numirecs_asc, order("numirecs ASC")
scope :sort_by_numirecs_desc, order("numirecs DESC")

In shops_controller.rb index method

@search = Shop.add_count_col.search(params[:search])
#etc.

In index.html.erb

<%= sort_link @search, :numirecs, "Inventory Records" %>

Found the sort_by__asc reference here: http://metautonomo.us/2010/11/21/metasearch-metawhere-and-rails-3-0-3/

命硬 2024-10-16 11:11:32

Rails 有一个内置的解决方案,称为 counter_cache

在您的商店表上创建一个名为“inventory_records_count”的表列。

class Shop < ActiveRecord::Base
  has_many :inventory_records, :counter_cache => true
end

http://asciicasts.com/episodes/23-counter-cache-column

Rails has a built-in solution for this called counter_cache

Create a table column named "inventory_records_count" on your shops table.

class Shop < ActiveRecord::Base
  has_many :inventory_records, :counter_cache => true
end

http://asciicasts.com/episodes/23-counter-cache-column

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文