Nested attributes problem in rails 3 with mongoid (nested objects aren`t saved)

601 views
Skip to first unread message

makaroni4

unread,
May 24, 2011, 9:37:08 AM5/24/11
to Mongoid
So I have a simple application as in RailsCast about nested forms. and
a problem is that when I submit the form (with survey and questions)
the questions are not saved.

My models (Survey, that has many Questions):

class Survey
include Mongoid::Document
field :name
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :allow_destroy => true
end

class Question
include Mongoid::Document
field :content
belongs_to :survey
end
and Survey Controller:

def new
@survey = Survey.new
3.times {@survey.questions.build}
....
and a view:

<%= form_for(@survey) do |f| %>
<%= f.fields_for :questions do |builder| %>
<%= builder.label :content, "Question" %><br />
<%= builder.text_area :content, :rows => 3 %><br />
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Question" %>
<% end %>
...
In my log I have:

Started POST "/surveys" for 127.0.0.1 at 2011-05-24 13:26:51 +0400
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"G£ô",
"authenticity_token"=>"tX0FfMiLbh1BwjuY4CuvAKt2UpTraY3vmdo58ocBnos=",
"survey"=>{"name"=>"
Rails", "questions_attributes"=>{"0"=>{"content"=>"Are you fond of
Rails?", "_destroy"=>"0"}, "1"=>{"content"=>"Rails is
the best, ha?", "_destroy"=>"0"}, "2"=>{"content"=>"How many
railscasts have you watched?", "_destroy"=>"0"}}}, "commit
"=>"Create Survey"}
MONGODB
nested_attributes_development['surveys'].insert([{"name"=>"Rails",
"_id"=>BSON::ObjectId('4ddb79dba5372914380000
69')}])
Redirected to http://localhost:3000/surveys/4ddb79dba537291438000069

makaroni4

unread,
May 24, 2011, 9:54:18 AM5/24/11
to Mongoid
For embedded relations it works fine, but even if I use "crutch" code
like this (to send array with objects) it doesn`t work ((

<% for question in @survey.questions -%>
<%= f.fields_for "questions_attributes[]", question do |builder|%>
<%= builder.text_field :content, :label => "Content"%>
<% end -%>
<% end -%>



On 24 май, 17:37, makaroni4 <makaro...@gmail.com> wrote:
> So I have a simple application as in RailsCast about nested forms. and
> a problem is that when I submit the form (with survey and questions)
> the questions are not saved.
>
> My models (Survey, that has many Questions):
>
> class Survey
>   include Mongoid::Document
>   field :name
>   has_many :questions, :dependent => :destroy
>   accepts_nested_attributes_for :questions, :allow_destroy => true
> end
>
> class Question
>   include Mongoid::Document
>   field :content
>   belongs_to :survey
> end
> and Survey Controller:
>
> def new
>     @survey = Survey.new
>     3.times {...@survey.questions.build}

prabesh shrestha

unread,
May 25, 2011, 4:03:42 AM5/25/11
to Mongoid
MONGODB
nested_attributes_development['surveys'].insert([{"name"=>"Rails",
"_id"=>BSON::ObjectId('4ddb79dba5372914380000
69')}])
This does not save to question collections which is not done with
mongoid in default.

Just add has_many :questions, :dependent => :destroy , :autosave =>
true

This should work.

makaroni4

unread,
May 25, 2011, 4:09:23 AM5/25/11
to Mongoid
It works!!! Thanks a lot!!!

I read documentation for active record, where :autosave option is true
and completely forgot to check it in mongoid.

The problem is solved)
Reply all
Reply to author
Forward
0 new messages