Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create the view for the has_one association?

So, imagine I have this model:

class Car
  has_one :engine
end

and the engine model:

class Engine
  belongs_to :car
end

When I present the form for the user, so that he can create a new car, I only want to allow him to select from one of the available engines ( the available engines will be in a select, populated by the collection_select ). The thing is, if I build the field like this:

<%= f.collection_select :engine,Engine.all,:id,:name %>

When I will try to save it, I will get an AssociationTypeMismatch saying that it expected an Engine, but it received a string.

Is this the way to do it?

def create
  car = Car.new(params[:car])
  engine = Engine.find(params[:engine])
  car.engine = engine
  if car.save
     # redirect somewhere
  else
     # do something with the errors
  end
end

I always felt that stuff, like associating an engine to a car, are done automatically by Rails, but I don't know how to make him do it.

Is switching the has_one and belongs_to associations the only way to achieve this?

I am lost and I feel like I'm missing something very basic here.

like image 671
Geo Avatar asked Apr 06 '11 20:04

Geo


2 Answers

You should use engine_id

<%= f.collection_select :engine_id, Engine.all, :id, :name %>

UPD

as far as Engine is not belongs_to Car so you shoulduse Nested Attributes here. This screencast will be very useful for you:

  • http://railscasts.com/episodes/196-nested-model-form-part-1
  • http://railscasts.com/episodes/197-nested-model-form-part-2

Checkout api: http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for

Short intro:

class Car
  has_one :engine
  accepts_nested_attributes_for :engine
end

and in your form:

<%= form_for @car ... do |f| %>
  ...
  <%= f.fields_for :engine do |b| %>
    <%= b.collection_select :id, Engine.all, :id, :name %>
    ...
  <% end %>
  ...
<% end %>
like image 146
fl00r Avatar answered Sep 20 '22 12:09

fl00r


I faced the same problem and here is my solution (improvements based on the fl00r answer):

  1. Do everything as fl00r have said.

  2. Add optional: true to the class description

class Engine belongs_to :car, optional: true end

More info at: http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html

  1. Modify
<%= f.fields_for :engine do |b| %>
  <%= b.collection_select :id, Engine.all, :id, :name %>
  ... 
<% end %>

to

<%= f.fields_for :engine, :include_id => false do |b| %>
  <%= b.collection_select :id, Engine.all, :id, :name %>
  ...
<% end %>

More info at: Stop rails from generating hidden field for fields_for method

  1. Modify your EngineController

3.1. Modify

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

to

def car_params
  params.require(:car).permit(:name, engine_attributes: [:id, :name])
end

More info at: Rails 4 Nested Attributes Unpermitted Parameters

3.1. Add a function (private)

def set_engine

  engine_id = car_params[:engine_attributes][:id]

  @engine = Engine.find(engine_id)

  @car.engine = @engine

  @car.save
end

3.2. Modify EngineController#update

  def update
    respond_to do |format|

      if @car.update(car_params)
        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

to

  def update
    respond_to do |format|

      if set_engine && @car.update(car_params)
        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
like image 31
prograils Avatar answered Sep 22 '22 12:09

prograils