我跟隨tutorial並有user
,hotel
和rating
模型。用戶可以創建酒店,用戶可以對它們評分。用戶評分值連同user_id
和hotel_id
一起記錄在表rating
中。當我渲染部分<%= render "hotels/hotels_list", :@hotels => Hotel.all %>
它顯示的酒店列表以及它們平均評分模型計算hotel
型號Hotel.rb:表格列自動計算平均評分
class Hotel < ActiveRecord::Base
attr_accessible :user_id
belongs_to :user
has_many :ratings
has_many :raters, :through => :ratings, :source => :users
def average_rating
@value = 0
self.ratings.each do |rating|
@value = @value + rating.value
end
@total = self.ratings.size
'%.2f' % (@value.to_f/@total.to_f)
end
end
型號User.rb:
class User < ActiveRecord::Base
has_many :hotels
has_many :ratings
has_many :rated_hotels, :through => :ratings, :source => :hotels
end
型號Rating.rb:
class Rating < ActiveRecord::Base
attr_accessible :value
belongs_to :user
belongs_to :hotel
end
我需要按照平均評分對酒店列表進行排序,也許需要在酒店模型中添加一些列即average_rating
這樣的列即average_rating
方法的平均值,這樣比我可以輕鬆訪問它。我該如何解決這個問題? RatingsController.rb
class RatingsController < ApplicationController
before_filter :authenticate_user!
def create
@hotel = Hotel.find_by_id(params[:hotel_id])
@rating = Rating.new(params[:rating])
@rating.hotel_id = @hotel.id
@rating.user_id = current_user.id
if @rating.save
respond_to do |format|
format.html { redirect_to hotel_path(@hotel), :notice => "Your rating has been saved" }
format.js
end
end
end
def update
@hotel = Hotel.find_by_id(params[:hotel_id])
@rating = current_user.ratings.find_by_hotel_id(@hotel.id)
if @rating.update_attributes(params[:rating])
respond_to do |format|
format.html { redirect_to hotel_path(@hotel), :notice => "Your rating has been updated" }
format.js
end
end
end
end