title: Rails, How i can create grouped_select?
body: <p>I'm new in rails and i need a help.
  I wanna to do a grouped selection on rails but i dont know how i can do it.</p>
  
  <p>i have 3 db tables cars, car_brands and car_models. When i add new car i need to select car model, how i can do it with gtouped selection.</p>
  
  <blockquote>
    <p>models/car_brand.rb</p>
  </blockquote>
  
  <pre><code>class CarBrand &lt; ActiveRecord::Base
    has_many :cars
    has_many :car_models
  
    mount_uploader :logo, CarBrandImgUploader
  end
  </code></pre>
  
  <blockquote>
    <p>models/car_model.rb</p>
  </blockquote>
  
  <pre><code>class CarModel &lt; ActiveRecord::Base
    has_many :cars
    belongs_to :car_brand
  
  end
  </code></pre>
  
  <blockquote>
    <p>cars_controller.rb</p>
  </blockquote>
  
  <pre><code>class CarsController &lt; ApplicationController
  
    before_action :set_car, only: [:show, :edit, :update, :destroy]
    before_action :set_model, only: [:index, :new, :edit]    
  
    # GET /cars
    # GET /cars.json
    def index
      @cars = Car.all
  
    end
  
    # GET /cars/1
    # GET /cars/1.json
    def show
    end
  
    # GET /cars/new
    def new
      @car = Car.new
    end
  
    # GET /cars/1/edit
    def edit
    end
  
    # POST /cars
    # POST /cars.json
    def create
      @car = Car.new(car_params)
  
      respond_to do |format|
        if @car.save
          if params[:ImagesCars]
            params[:ImagesCars]['image'].each do |a|
              @ImagesCar = @car.ImagesCars.create!(:image =&gt; a, :car_id =&gt; @car.id)
            end
          end
          format.html { redirect_to @car, notice: 'Car was successfully created.' }
          format.json { render :show, status: :created, location: @car }
        else
          format.html { render :new }
          format.json { render json: @car.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /cars/1
    # PATCH/PUT /cars/1.json
    def update
      respond_to do |format|
        if @car.update(car_params)
          if params[:ImagesCars]
            params[:ImagesCars]['image'].each do |a|
              @ImagesCar = @car.ImagesCars.create!(:image =&gt; a, :car_id =&gt; @car.id)
            end
          end
          format.html { redirect_to @car, notice: 'Car was successfully updated.' }
          format.json { render :show, status: :ok, location: @car }
        else
          format.html { render :edit }
          format.json { render json: @car.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /cars/1
    # DELETE /cars/1.json
    def destroy
      @car.destroy
      respond_to do |format|
        format.html { redirect_to cars_url, notice: 'Car was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_car
        @car = Car.find(params[:id])
      end
  
      def set_model
        @models = CarModel.all
        @brands = CarBrand.all
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def car_params
        params.require(:car).permit(:title_en, :keys_en, :description_en, :text_en, :title_fr, :keys_fr, :description_fr, :text_fr, :title_ru, :keys_ru, :description_ru, :text_ru, :title_es, :keys_es, :description_es, :text_es, :model_id, :brand_id, :price_day, :price_week, :p_info, :images_cars =&gt; [:id, :car_id, :image])
      end
  end
  </code></pre>
  
  <blockquote>
    <p>cars/new.html.haml</p>
  </blockquote>
  
  <pre><code>= simple_form_for(@car, html: { role: 'form', multipart: true }) do |f|
   = f.input :price_day, as: :integer, input_html: {class: 'form-control', placeholder: 'Price Day ej: 150'}
   = f.cktext_area :text_en, as: :text, input_html: { class: 'form-control' },  :label =&gt; 'EN Website Content Text EN'
   =f.input :model_id, collection: @models, as: :grouped_select, group_method: :brand_id
  = f.submit 'Save'
  </code></pre>
  
  <p>i get this error:
  undefined method `map' for nil:NilClass</p>
  
  <p>Please help or explain how i can do the grouped selection. 
  Thx</p>