embeds_one and fields_for

183 views
Skip to first unread message

Jeff

unread,
Aug 23, 2010, 5:56:59 PM8/23/10
to Mongoid
Very simple schema, Rails3 RC, mongoid beta 15

Just can't seem to get fields_for to work or render anything. I have
a record with an address in it, which doesn't show for editing. New/
create doesn't seem to work, even when I force @employee.address =
Address.new.

What's the secret that I am missing?


--- view

= form_for @employee do |form|
= form.text_field :first_name
= form.text_field :last_name
- form.fields_for :address do |af|
= af.text_field :street
= submit_tag 'Save'

--- models

class Employee
include Mongoid::Document

field :first_name
field :last_name
embeds_one :address

validates_presence_of :first_name, :last_name
accepts_nested_attributes_for :address
end

class Address
include Mongoid::Document
field :street
field :city
field :state
field :post_code
embedded_in :employee, :inverse_of => :address
end

Ahmad

unread,
Aug 23, 2010, 6:27:51 PM8/23/10
to mon...@googlegroups.com
are you building the object in the controller?

@employee.address.build

Gagan Shrestha

unread,
Aug 24, 2010, 8:05:32 AM8/24/10
to mon...@googlegroups.com
You can try this too.
@employee.build_address  in your Employee controller



--
with Regards
Gagan Shrestha
Web developer
Sprout Tech Pvt Ltd
Lalitpur, Nepal
+977 1 559-2701

Jeff

unread,
Aug 25, 2010, 1:01:19 PM8/25/10
to Mongoid
Neither @employee.build_address or @employee.address.build (when the
address is present) seem to work. fields_for and nested forms just
plain don't work for me. I find it hard to believe I'm the only one
having this problem, doesn't anybody else use nested forms?

Rick Frankel

unread,
Aug 25, 2010, 6:05:53 PM8/25/10
to mon...@googlegroups.com
On Wed, Aug 25, 2010 at 10:01:19AM -0700, Jeff wrote:
> Neither @employee.build_address or @employee.address.build (when the
> address is present) seem to work. fields_for and nested forms just
> plain don't work for me. I find it hard to believe I'm the only one
> having this problem, doesn't anybody else use nested forms?
>

I found them difficult to get working as well. In general I have found
the "dirty data only" "optimization" a bit too agressive. I was able
to get embedded documents and nested forms to work reliable as follows
(YMMV):

1. monkey patched mongoid w/ an "init_embedded" method
Module Mongoid
module Document
def init_embedded(field, validate=false)
return unless send(field).nil?

klass = options[:klass]||field.classify.constantize

obj = klass.new
send("#{field}=", obj)

obj.save(validate) unless new_record?
end
end
end

2. In a before filter on my controller call init_embedded on each
embedded class (recursively).

class AppController < ApplicationController
before_filter :init_embedded
# ...

def init_embedded
@app.init_embedded('user_info')
@app.user_info.init_embedded('address')

return true
end

end

3. assign each embedded document from the params separately before
saving

class AppController < ApplicationController
def save
@app.attributes=params[:app])
@app.user_info.attributes=params[:app][:user_info]

@app.save
end
end

This worked for me, it may not be the "correct" way but it was the
only way I could get the data to stick.

rick

Reply all
Reply to author
Forward
0 new messages