<file_sep>class CarsController < ApplicationController

  
    def index
        cars = find_company(params[:company_id]).cars.select(:id, :name, :price)
        render json: cars, status: :ok
    end

    def show
        car = find_car(params[:id])
        render json: car, status: :ok
    end

    def create
        car = find_company(params[:company_id]).cars.new(car_params)
        if car.save
            car = find_car(car.id)
            render json: car, status: 201
        else
            render json: {errors: car.errors}, status: :unprocessable_entity
        end
    end

    def update
        car = Car.find(params[:id])
        if car.update_attributes(car_params)
            car =car(params[:id])
            render json: car, status: :ok
        else
            render json: {errors: car.errors}, status: :unprocessable_entity
        end
    end

    def destroy
        car = Car.find(params[:id])
        car.destroy
        render json: car, status: 204
    end

    private

    def car_params
        params.require(:car).permit(:name, :price)
    end

    def find_company company_id
        Company.find(company_id)
    end
    def find_car id
        Car.select(:id, :name, :price).where(id: id)
    end
end<file_sep>с1 = Company.create({name: 'MMM'})
c2 = Company.create({name: 'KFC'})

c1.cars.create({name: '<NAME>', price: 627000})
c1.cars.create({name: '<NAME>', price: 337000})
c1.cars.create({name: 'Lada 1488', price: 145000})

c2.cars.create({name: '<NAME>', price: 228000})
c2.cars.create({name: '<NAME>', price: 181000})
c2.cars.create({name: 'Porche 911', price: 8450000})
<file_sep>class CompaniesController < ApplicationController
    def index
        companies = Company.left_outer_joins(:cars)
                            .select('companies.id, companies.name, COUNT(cars.id) AS cars_count')
                            .group('companies.id, companies.name')
        render json: companies, status: :ok
    end

    def create
        company = Company.new(com_params)
        if company.save
            company = find_company(company.id)
            render json: company, status: 201
        else 
            render json: {errors: company.errors}, status: unprocessable_entity

    end

    def show
        company = find_company(params[:id])
        render json: company, status: :ok
    end

    def update
        company = Company.find(params[:id])
        if company.update_attributes(com_params)
            render json: company, status: :ok
        else
            # Что-то пошло не так
            render json: {errors: company.errors}, status: :unprocessable_entity
        end
    end

    def destroy
        company = company.find(params[:id])
        company.destroy
        render json: company, status: 204
    end

    private
        def com_params
            params.require(:company).permit(:name)
        end

        def find_company id
            Company.left_outer_joins(:cars)
                                .select('companies.id, companies.name, COUNT(cars.id) AS cars_count')
                                .where(id: id)
                                .group('categories.id, categories.name')         

        end
    end
end
<file_sep>class Company < ApplicationRecord
    has_many :cars, dependent: :destroy
    validates :name, presence: true
end