Ruby on Rails Saturday, September 1, 2012

[SOLVED] doesn't need this key ... has_one .. the key is in the
target model 'item_id' in Geolocation


On 1 sep, 12:57, Erwin <yves_duf...@mac.com> wrote:
> 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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment