红宝石轨道 - 如何按平均评级对记录进行排序



我有一个场地表,我在场地索引页面上显示为部分。我还有一个评论表,一个场所可以有很多评论,每个评论都有1-5分。

我正在尝试让场地显示在索引页面上,平均评分最高的场地位于顶部并下降。

控制器代码如下所示:

场地控制器

def index
    if
      @venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas]).joins(:reviews).order("reviews.rating DESC")
    else
      @venues = Venue.all
    end
  end

这给出了这样的结果:

  • 如果场地 1 有 5 星评论它在顶部显示场地部分列表。

  • 如果场地 2 有 5 星评价,并且1星评论显示两个部分,一个在顶部,一个
    在顶部在列表底部。

  • 如果场地 3 有 5 星评价、3 星评价和1星评论显示3颗部分,一个在顶部,一个在中间,一个在中间,一个在列表底部。

只想每个场地进行一次部分展示,但按平均评分在列表中排名,我觉得有一个 .average 或某处缺少的东西,我该如何实现这一目标?

感谢您的任何帮助,非常感谢!

编辑

场地模型

class Venue < ActiveRecord::Base
  attr_accessible :name, :addressline1, :addressline2, :addressline3, :addressline4, :postcode, :phonenumber, :about, :icontoppx, :iconleftpx, :area_id, :venuetype_id, :lat, :long, :venuephotos_attributes
  belongs_to :area
  belongs_to :venuetype
  has_many :reviews
  has_many :venuephotos
  accepts_nested_attributes_for :venuephotos, :allow_destroy => true
  scope :with_type, lambda { |types|
    types.present? ? where(:venuetype_id => types) : scoped }
  scope :with_area, lambda { |areas|
    areas.present? ? where(:area_id => areas) : scoped }
  def to_param
    "#{id}-#{name.gsub(/W/, '-').downcase}"
  end
  def add_rating(rating_opts)
    @venue.add_rating(:rating => rating, :reviewer => params[:rating][:reviewer])
    self.reviews.create(rating_opts)
    self.update_rating!
  end
  def update_rating!
    s = self.reviews.sum(:rating)
    c = self.reviews.count
    self.update_attribute(:average_rating, s.to_f / c.to_f)
    self.save(:validate => false)
  end
end

用于添加评论的开发日志

Started POST "/venues/44-rating-test-5/reviews" for 127.0.0.1 at 2011-05-18 09:24:24 +0100
  Processing by ReviewsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"GZWd67b5ocJOjwKI6z9nJInBXxvQahHrjUtUpdm9oJE=", "review"=>{"rating"=>"5", "title"=>"5 star review"}, "venue_id"=>"44-rating-test-5"}
  [1m[36mVenue Load (1.0ms)[0m  [1mSELECT `venues`.* FROM `venues` WHERE (`venues`.`id` = 44) LIMIT 1[0m
  [1m[35mUser Load (0.0ms)[0m  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (2.0ms)[0m  describe `reviews`
  [1m[36mAREL (0.0ms)[0m  [1mINSERT INTO `reviews` (`title`, `created_at`, `updated_at`, `venue_id`, `user_id`, `rating`) VALUES ('5 star review', '2011-05-18 08:24:24', '2011-05-18 08:24:24', NULL, 3, 5)[0m
  [1m[35mSQL (27.0ms)[0m  COMMIT
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mAREL (0.0ms)[0m  UPDATE `reviews` SET `venue_id` = 44, `updated_at` = '2011-05-18 08:24:24' WHERE (`reviews`.`id` = 90)
  [1m[36mSQL (23.0ms)[0m  [1mCOMMIT[0m
  [1m[35mSQL (1.0ms)[0m  SELECT COUNT(*) FROM `reviews` WHERE (`reviews`.venue_id = 44)
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1[0m
Rendered reviews/_review.html.erb (9.0ms)
Rendered reviews/create.js.erb (22.0ms)
Completed 200 OK in 220ms (Views: 56.0ms | ActiveRecord: 54.0ms)

编辑创建审阅方法(审阅控制器)

def create
    @review = current_user.reviews.create!(params[:review])
    @review.venue = @venue
    if @review.save
      flash[:notice] = 'Thank you for reviewing this venue!'
      respond_to do |format|
        format.html { redirect_to venue_path(@venue) }
        format.js
      end
    else
      render :action => :new
    end
  end

为了补充 NoICE 的答案,通过挂钩:after_add:after_remove关联回调,您不必记住调用特殊的add_rating方法。

class Venue < ActiveRecord::Base
  has_many :reviews, :after_add => :update_average_rating, :after_remove => :update_average_rating
  def update_average_rating(review=nil)
    s = self.reviews.sum(:rating)
    c = self.reviews.count
    self.update_attribute(:average_rating, c == 0 ? 0.0 : s / c.to_f)
  end
end

此外,您需要检查 0 的计数以防止被零除。

创建审阅时,必须追加 << 或将其concat到场地对象的reviews关联,以便触发回调。例如,这会将评论关联到场地,创建评论(插入到数据库中),并触发回调:

@venue = Venue.find(params[:venue_id])
@venue.reviews << Review.new(params[:review])

这将创建评论,但不会触发回调,即使venue_id是参数:

Review.create(params[:review])

如果确实想让操作触发回调,可以将代码更改为:

def create
  @review = Review.new(params[:review].merge({ :user => current_user, :venue => @venue })
  if @review.valid? and @venue.reviews << @review
  ...

但是,为了方便地修复它,您只需在带有flash[:notice]的行之前添加@review.venue.update_average_rating

如果我理解正确的话,你有模型场地,它有has_many:reviews,每个评论都有列"评级"。

我正在推荐替代代码,例如迈克尔给出的代码,它应该更快,准备好数百万条记录,但它需要一些处理,然后添加审查(在本例中介绍),当记录被选择、排序和显示时,它会给你巨大的性能提升:

创建一个迁移,将average_rating添加为浮点数:

add_collumn :venues, :average_rating, :float, :default => 0.0, :null => false
add_index :venues, :average_rating

现在,在您的控制器中:

# perhaps add paginate at the end instead of .all ...
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas]).order("average_rating DESC").all

模型更新:

class Venue < ActiveRecord::Base
  has_many :reviews
  # you'll need to create ratings for this venue via this method, so everything is atomic
  # and transaction safe
  # parameter is hash, so you can pass as many review parameters as you wish, e.g.
  # @venue.add_rating(:rating => rating, :reviewer => params[:rating][:reviewer])
  # or
  # @venue.add_rating(params[:rating])
  # :)
  def add_rating(rating_opts)
    # you can of course add as 
    self.reviews.create(rating_opts)
    self.update_rating!
  end
  # let's update average rating of this venue
  def update_rating!
    s = self.reviews.sum(:rating)
    c = self.reviews.count
    self.average_rating = s.to_f / c.to_f
    self.save(:validate => false)
    # or you can use .update_attribute(:average_rating, s.to_f / c.to_f)
  end
end

希望这有帮助。如果您有任何问题,请询问。

问候, NoICE

您可以在Venue上创建一个average_rating方法,然后简单地:

@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas]).includes(:reviews).sort_by(&:average_rating).reverse

方法:

class Venue
  def average_rating
    ratings = reviews.map(&:rating)
    ratings.sum.to_f / ratings.size
  end
end

如果有大量记录或性能至关重要,则此解决方案可能不是最佳解决方案,但它非常简单且有效。

让我们假设评论是可编辑的,那么没有答案效果很好。所以我做了如下。

class Venue < ActiveRecord::Base
    has_many :reviews, :dependent => :delete_all
end

现在在评论模型中如下所示。

class Venue < ActiveRecord::Base
    belongs_to :venue
    # If reviews are editable
    after_save :update_average_rating 
    # If reviews are deletable
    before_destroy :update_average_rating 
    private
      def update_average_rating
       s = self.venue.reviews.sum(:rating)
       c = self.venue.reviews.count
       self.venue.update_attribute(:average_rating, c == 0 ? 0.0 : s / c.to_f)
  end
end

相关内容

  • 没有找到相关文章

最新更新