Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested form, embedded docs, mongoid 2.2.0 problem

I cant seem to find answer for this here or with the google, any help would be awesome. The Building saves correctly, but the embedded doc PriorityArea doesnt get updated... I want to eventually have it ajax a new form for new priority areas evenutally, but need it to update first.

Mongoid::Errors::InvalidFind in BuildingsController#update

Calling Document#find with nil is invalid

class Building
  include Mongoid::Document
  embeds_many :priority_areas
  accepts_nested_attributes_for :priority_areas, :allow_destroy => true, :autosave => true
end

class PriorityArea
  include Mongoid::Document
  embedded_in :building, :inverse_of => :priority_areas
end

#view

= form_for [@customer, @building] do |f|
  ...
  ...
  = f.fields_for :priority_areas do |pa|
    = f.name
    ...
    ...

#controller

@building.update_attributes(params[:building])

It correctly yeilds the correct data from the db, but fails to error above on building#update. Any help is greatly appreciated.


update in the building#update im puts params[:building][:priority_areas_attributes].to_yaml

which yeilds

--- !map:ActiveSupport::HashWithIndifferentAccess 
"0": !map:ActiveSupport::HashWithIndifferentAccess 
name: area 51
location: near front door
notes: ""
priority: "1"
id: ""
"1": !map:ActiveSupport::HashWithIndifferentAccess 
name: area 52
location: near rear door
notes: ""
priority: "2"
id: ""

im guessing the problem is the null id:""

like image 755
Rockman Avatar asked Feb 21 '26 09:02

Rockman


1 Answers

The problem was the null id it needed to have an ObjectId to work correctly. stupid error on my part.

like image 154
Rockman Avatar answered Feb 26 '26 04:02

Rockman