Fix Review and Statistic Score on the Restaurant Page
Purpose
Use this script when review statistics or selected reviews on restaurant pages are inconsistent or stale. It rebuilds review stat records and selected review records.
Execution environment
Run in Rails console with appropriate production/staging permissions.
rails c --production
Script
Restaurant.active.not_expired.find_each do |restaurant|
# Remove old selected review mappings and review stats.
RestaurantSelectedReview.where(review_stat: Restaurants::ReviewStat.where(restaurant: restaurant)).destroy_all
Restaurants::ReviewStat.where(restaurant: restaurant).destroy_all
# Rebuild review stats and selected reviews.
Restaurants::ReviewStatGenerator.new.perform(restaurant.id, {}, 'migrate')
Restaurants::SelectedReviewGenerator.new.perform(restaurant.id, {}, 'migrate')
# Refresh cache key to expose updated data.
restaurant.refresh_view_cache_key
end
Verification
- Open the restaurant page in admin/web.
- Check rating, review count, and selected reviews.
- Confirm data is aligned with source reviews.
Safety and rollback
- Run for a limited subset of restaurant IDs first.
- If output is unexpected, rerun stat generators for affected IDs after reverting bad source data.
Caption: Example of inconsistent score before regeneration.
Caption: Example of corrected score after regeneration.