I have an Item model with has_one :geolocation association I need to
create/update in one form
class Item < ActiveRecord::Base
has_one :geolocation, :dependent => :destroy
accepts_nested_attributes_for :geolocation, :reject_if
=> :all_blank, :allow_destroy => true
attr_accessible :geolocation_attributes
…
end
class Geolocation < ActiveRecord::Base
belongs_to :item
…
end
as stated in all docs
in my Items table migration , I have a geolocation_id ( has_one
association )
and in my Geolocations table , I have an item_id ( belongs_to
association )
Creating the item in the controller
def new
@item = Item.new
@item.geolocation = Geolocation.new
respond_with(@item)
end
def create
@item = Item.new(params[:item])
if @item.save
redirect_to backoffice_item_path(@item), notice:
t(:item_created)
else
render :new
end
end
with a nested form
…
= simple_form_for @item, :url => backoffice_items_path, :html =>
{:class => ‘form-vertical’ } do |f|
…
= f.fields_for :geolocation do |geolocation|
inserts correctly the item record and the geolocation record BUT in
the item record, the geolocation_id is NOT set …
as if that the has_one association was not taken in account…
I’m surely missing something important , but I don’t see what …
feedback and cue will be a big +++
thanks