You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: hostgroups_controller.rb
    path of file: ./repos/foreman/app/controllers/api/v2
    
    the code of the file until where you have to start completion: module Api
  module V2
    class HostgroupsController < V2::BaseController
      include Api::Version2
      include
    
      def rebuild_config
        results = @hostgroup.recreate_hosts_config(params[:only], params[:children_hosts])
        failures = []
        results.each_pair do |host, result|
          host_failures = result.reject { |key, value| value }.keys.map { |k| _(k) }
          failures << "#{host}(#{host_failures.to_sentence})" unless host_failures.empty?
        end
        if failures.empty?
          render_message _("Configuration successfully rebuilt."), :status => :ok
        else
          render_error :custom_error, :status => :unprocessable_entity,
                       :locals => { :message => _("Configuration rebuild failed for: %s." % failures.to_sentence) }
        end