Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dgorodnichy/7693754fe0b95cb040743570e9edb1ba to your computer and use it in GitHub Desktop.
Save dgorodnichy/7693754fe0b95cb040743570e9edb1ba to your computer and use it in GitHub Desktop.
class FilmsController < ApplicationController
def index
@films = serialized_films
render json: @films
end
private
def films_scope
Film.all
end
def serialized_films
films_scope.map do |film|
{ title: film.title, genre: film.genre, rate: films_rates[film.id] }
end
end
def films_rates
@films_rates ||= films_scope.includes(:reviews).group(:film_id).average(:rate)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment