I have following models:
Product:
class Product < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :recommendations, :through => :product_recommendations
end
ProductRecommendation:
class ProductRecommendation < ActiveRecord::Base
belongs_to :recommendation
belongs_to :product
end
Recommendation:
class Recommendation < ActiveRecord::Base
has_many :product_recommendations, :dependent => :destroy
has_many :products, :through => :product_recommendations
has_many :recommendation_ratings, :dependent => :destroy
has_many :ratings, :through => :recommendation_ratings
end
Rating:
class Rating < ActiveRecord::Base
has_many :recommendation_ratings, :dependent => :destroy
has_many :recommendations, :through => :recommendation_ratings
end
RecommendationRating:
class RecommendationRating < ActiveRecord::Base
belongs_to :recommendation
belongs_to :rating
end
How would I be able to calculate the average rating for a given recommendation? My ratings table contains just 4 records, (Ratings 1-4), and I have an action in my recommendation controller which updates the RecommendationsRatings join table to associate a rating with a recommendation. Thanks!
have you tried:
and then
this is assuming the Ratings model has an int / float called rating which stores the rating