Ruby on rails 使用其他模型的值对_进行分组

Ruby on rails 使用其他模型的值对_进行分组,ruby-on-rails,Ruby On Rails,使用ROR 2.3.8 我在城市\u controller.rb中有这个: @shops = Shop.published.search params[:keyword], { :conditions => conditions, :star => true, :group_by => 'city_id', :group_function => :attr, :page

使用ROR 2.3.8

我在
城市\u controller.rb中有这个:

@shops = Shop.published.search params[:keyword], {
      :conditions     => conditions,
      :star           => true,
      :group_by       => 'city_id',
      :group_function => :attr,
      :page           => params[:page]
    }.merge(:order => 'rating_average DESC')
@cities = @shops.collect { |shop| shopy.city }
我如何告诉Rails从城市模型而不是商店模型中获得
评级\u平均值
?因为商店模型没有
评级\u平均值
。事实上,这是《城市》
模型获得评级

多谢各位

更新

已在
Shop.rb中发布
namescope

 sphinx_scope(:published) { 
  {:conditions => {:status => 'published'}}
}
  define_index do
    indexes city.name, :as => :name, :sortable => true
    indexes city.duration, :as => :duration
    indexes city.status, :as => :status
    #has city.budget, :as => :budget
    #has city(:created_at), :as => :created_at
    has city(:rating_average), :as => :rating_average
    has city_id
  end
Shop.rb中的索引

 sphinx_scope(:published) { 
  {:conditions => {:status => 'published'}}
}
  define_index do
    indexes city.name, :as => :name, :sortable => true
    indexes city.duration, :as => :duration
    indexes city.status, :as => :status
    #has city.budget, :as => :budget
    #has city(:created_at), :as => :created_at
    has city(:rating_average), :as => :rating_average
    has city_id
  end
更新2

class City < ActiveRecord::Base
  has_many :shops, :dependent => :destroy    
  ...
end
class City:破坏
...
终止

您应该使用连接来实现以下目标:

@shops = Shop.published.search params[:keyword], {
      :conditions     => conditions,
      :star           => true,
      :group_by       => :city_id,
      :group_function => :attr,
      :page           => params[:page]
    }.merge(:order => :rating_average,
            :sort_mode => :desc)

您还应该在列之前添加
城市。
商店。
以指定表的列。

请参阅更新。我不认为这是问题所在,因为我已经删除了
发布的
,但仍然无法产生结果。
评级(u average)
城市
表中的一列还是
城市
模型中的一种方法?等等,让我先重建我的索引。我将再次更新PI没有更改
:订单
,只更改了
商店。city\u id
,但没有返回结果
rating\u average
City
模型中的一列。我不知道斯芬克斯,但试试这个:
Shop.all:joins=>:City,:order=>“cities.rating\u average DESC”
。你得到结果了吗?确实应该使用连接,然后添加城市。在评分之前,你的平均分是多少。但你的联想必须是正确的。能否显示您在商店模型中定义的与城市的关联。@Michael Torfs,请参阅更新。