Mongoid - MongoDB Embedded Documents and Rails3 Form Helpers

535 views
Skip to first unread message

blaines

unread,
Jun 14, 2010, 10:25:11 AM6/14/10
to Mongoid
Hey there, Mongoid is great, awesome work so far!
I'm making a simple little app that is making use of many embedded
documents but I can't seem to get rails' form helpers to cooperate
with it. So far I've hacked my way into making it work with a hidden
field and my own rendition of update_attributes. Is there a better way
to get embedded document support in rails3?

James

unread,
Jun 14, 2010, 12:40:06 PM6/14/10
to mon...@googlegroups.com, Mongoid
I am very interested inthis as well. I am a ruby/rails newbie so I
could hack my way to a workaround as you did.

There was a bug opened on rails but was closed as will not fix. I
don't have the link handy but will find it later.

Sent from my iPhone

Lorenzo Planas

unread,
Jun 15, 2010, 3:28:52 AM6/15/10
to Mongoid
Haven't been using Rails 3 for a while, but from my memories:

Say you have Company with embedded Address documents and the following
code:

# _address_form.html.erb
form_for [@company, @address] do |f|
f.text_field :street
... and the usual helpers
end

# Address Controller - new action
# Assuming we have nested resources, which fit well with MongoDB
document-orientation
@company = Company.find(params[:company_id])
@address = @company.addresses.build

# AddressController - create action
@company = Company.find(params[:company_id])
@address = @company.addresses.build(params[:address])
if @address.save
....

# Address Controller - edit action
@company = Company.find(params[:company_id])
@company.addresses.each {|a| @address = a if a.id == params[:id]}

# AddressController - update action
@company = Company.find(params[:company_id])
@company.addresses.each {|a| @address = a if a.id == params[:id]}
if @address.update_attributes(params[:address])
...

You can also create / update addresses from Company's controller by
including this in Company's model:
accepts_nested_attributes_for :addresses

Is this of any help?

Lorenzo

On Jun 14, 6:40 pm, James <jhicks0...@gmail.com> wrote:
> I am very interested inthis as well.  I am a ruby/rails newbie so I  
> could hack my way to a workaround as you did.
>
> There was a bug opened on rails but was closed as will not fix.  I  
> don't have the link handy but will find it later.
>
> Sent from my iPhone
>

James Hicks

unread,
Jun 16, 2010, 1:12:17 PM6/16/10
to mon...@googlegroups.com
Interesting. I will try this. BTW, what does the @company.addresses.build do?

Here is the link about the fields_for & mongoid
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/823a569f5b456d62?pli=1

camcaine

unread,
Jun 16, 2010, 3:01:49 PM6/16/10
to Mongoid
In ActiveRecord forms build will add the association for you,

i.e. @company.addresses.build would add company_id to the address
model

In mongoid I am guessing that it adds it to the embedded_in array
instead of doing this:

@address = Address.new
@company << @address

I could be wrong though, I haven't look ed at the source code much.

Cameron

On Jun 16, 6:12 pm, James Hicks <jhicks0...@gmail.com> wrote:
> Interesting.  I will try this.  BTW, what does the @company.addresses.build do?
>
> Here is the link about the fields_for & mongoidhttp://groups.google.com/group/rubyonrails-core/browse_thread/thread/...

camcaine

unread,
Jun 16, 2010, 4:08:22 PM6/16/10
to Mongoid
that should have been @company.addresses << @address
Reply all
Reply to author
Forward
0 new messages