Nested Document Forms

237 views
Skip to first unread message

jmcervera

unread,
Jan 12, 2010, 6:11:23 AM1/12/10
to MongoMapper
Hello,

Is there a way to specify something like accept_nested attributes_for
for embedded documents, and easy design a Nested Document Form as in
ActiveRecord?

Anybody knows if thIs this planned for the future?
Thanks

John Nunemaker

unread,
Jan 12, 2010, 10:56:22 AM1/12/10
to mongo...@googlegroups.com
Nope and nope. It is really nit that hard with attr_accessor's.

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

John Nunemaker

unread,
Jan 12, 2010, 10:56:41 AM1/12/10
to mongo...@googlegroups.com
I'm nit opposed. Just don't have plans.

On Jan 12, 2010, at 6:11 AM, jmcervera <juanma...@gmail.com> wrote:

Chris Hanks

unread,
Jan 12, 2010, 12:25:23 PM1/12/10
to MongoMapper
I've been having a lot of problems with this recently. One solution
I've found is to include :index => nil on your fields_for
declarations, but this wreaks havoc with radio buttons (multiple
children will all share the same options), so in my situation it's not
really usable.

If anyone has a good generic solution for handling a document and many
embedded documents in a single form, though, I'd really like to see
it. I've been hacking around it in my controllers, and it's been very
ugly.

Lance Carlson

unread,
Jan 12, 2010, 1:37:29 PM1/12/10
to mongo...@googlegroups.com
to get nested attributes to work in datamapper, I had to do this: (it
assumes you're using rails.. maybe you can port it to work with
mongomapper)

def accepts_nested_attributes_for(*args)
arg = *args.pop
target_model = arg.to_s.classify
is_many = !arg.to_s.grep(/[^s]s$/).empty?

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{arg}_attributes
instance_variable_get("@#{arg}_attributes")
end

def #{arg}_attributes=(value)
new_value = []
value.each {|key, attrs|
new_value << #{target_model}.new(attrs)
}

new_value = new_value.first unless #{is_many}

send("#{arg}=", new_value)
end
RUBY
accepts_nested_attributes_for(*args) unless args.empty?
end

Zef

unread,
Jan 12, 2010, 3:51:41 PM1/12/10
to MongoMapper
I'm actually working on this... my implementation (based on
ActiveRecord) is definitely not done, but it works basically right
now.

A warning though: if you mark a related document for destruction (by
assigning "_destroy" => true), it will be destroyed even if the
document is not saved. This is only for associated documents (not
embedded docs). I have an idea of how to change this, but it will take
some more work to get there.

Feel free to use if you want, but no guarantees... Tests aren't
complete (and one is failing now), but that's why I haven't released
this publicly yet.

http://gist.github.com/275594

Hope this helps!

jmcervera

unread,
Jan 18, 2010, 12:00:55 PM1/18/10
to MongoMapper
Thanks Zef

Based in what you've done, I have ported the activerecord version

https://gist.github.com/d313817a57fd079e4df7

It works pretty well for me, I include MongoMapper::NestedAttributes
in every Document and EmbeddedDocument that need nested attributes.
I think it would be nice that something like this will be merged in
MongoMapper and have the functionality already included.

Juanma

Chris Hanks

unread,
Jan 18, 2010, 8:49:54 PM1/18/10
to MongoMapper
Wow, awesome work all around. I'm using Juanma's gist right now and
it's working great.

I'd really like to see this in MongoMapper core as well.

Thanks a lot!

Manuel

unread,
Jan 19, 2010, 5:43:50 AM1/19/10
to MongoMapper
I already have a nested form for my embedded-documents, and it works
great with :index => nil.

But somehow @parent.update_attributes(params[:parent-with-embedde-
docs] doesn't work....

It works for the parent object, the embedded-documents get updated
too, but every value that isn't sent with the form is overrridden with
nil....
But creating a new Record like this works...

Maybe somebody else has an idea.

Regards,
Manuel

Manuel

unread,
Jan 20, 2010, 3:50:07 AM1/20/10
to MongoMapper
It's pretty obvious that a new record creation works like that ^^

Is update_attributes designed to also update embedded_documents?
Or do I have to overwrite update_attributes or use the patch from
Juanma?

I just thought that this works without a patch, because the creation
of a new record works perfect...

I use the latest master branch.

Thanks for every hint :-)

Reply all
Reply to author
Forward
0 new messages