Keith Johnson
Keith Johnson

Reputation: 730

Fields_for has_many associations not saving

So I have a deeply nested association among Polls, Questions, and Answers:

class Poll < ActiveRecord::Base
  attr_accessible :description, :end_time, :start_time, :title, :user_id, :questions_attributes, :is_live

  belongs_to :user
  has_many :questions, :dependent => :destroy
  # This is the plularized form of sms, it's not smss
  has_many :sms, :through => :questions 
  has_many :feedbacks

  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
end

class Question < ActiveRecord::Base
  attr_accessible :poll_id, :title, :answer_attributes

  belongs_to :poll
  has_many :answers, :dependent => :destroy
  # THis is the plularized form of sms, it's not smss
  has_many :sms
                                          #If someone creates a form with a blank question field at the end, this will prevent it from being rendered on teh show page
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true

end

class Answer < ActiveRecord::Base
  attr_accessible :is_correct, :question_id, :title

  belongs_to :question
end

And am trying to save all objects in a single fields_for form like so.

= form_for @poll, :class=>'create-poll-form' do |f|
  = f.text_field :title, :autofocus => true, :placeholder => "Poll Title"
  = f.text_field :description, :placeholder => 'Description'  
  = f.fields_for :questions do |builder| 
    = render "questions/question_fields", :f => builder

  = f.submit "Create Poll", :class => 'btn btn-danger'

Questions Partials

%p
  = f.label :title, "Question"
  = f.text_field :title
  = f.check_box :_destroy
  = f.label :_destroy, "Remove Question"
%p
  = f.fields_for :answers do |builder|
    = render "answers/answer_fields", :f => builder

Answers Partial

%p
  = f.label :title, "Answer"
  = f.text_field :title
  = f.check_box :_destroy
  = f.label :_destroy, "Remove Answer"

Yet the PollsController isn't persisting the data:

  def create
    binding.pry
    @poll = current_user.polls.new(params[:poll])
    # @poll = Poll.create(params[:poll])
    binding.pry
    @poll.save!
    redirect_to root_path
  end

  def new
    @poll = Poll.new  

    1.times do
      question = @poll.questions.build
      2.times {question.answers.build}
    end
  end

Any tips here would be amazing, I've been working on this for a bit and it's stumping me! Thanks in advance!

Also here's the server log:

Started POST "/polls" for 127.0.0.1 at 2013-06-06 20:14:47 -0400
Processing by PollsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hk+KuNsTLx3sH9pE7Mf8XETGsmxTsRN4/tWUBn3CIVE=", "poll"=>{"title"=>"Testing 1-2-1-2", "description"=>"Up on the mic", "questions_attributes"=>{"0"=>{"title"=>"Cake or Death?", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"title"=>"Cake", "_destroy"=>"0"}, "1"=>{"title"=>"Death", "_destroy"=>"0"}}}}}, "commit"=>"Create Poll"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Question Load (0.5ms)  SELECT "questions".* FROM "questions" WHERE "questions"."poll_id" IS NULL
   (0.3ms)  BEGIN
  SQL (47.6ms)  INSERT INTO "polls" ("created_at", "description", "end_time", "is_live", "start_time", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["created_at", Fri, 07 Jun 2013 00:16:46 UTC +00:00], ["description", "Up on the mic"], ["end_time", nil], ["is_live", nil], ["start_time", nil], ["title", "Testing 1-2-1-2"], ["updated_at", Fri, 07 Jun 2013 00:16:46 UTC +00:00], ["user_id", 1]]
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 170668ms (ActiveRecord: 54.9ms)

Upvotes: 0

Views: 985

Answers (1)

muttonlamb
muttonlamb

Reputation: 6501

I'm not sure if this is the problem but in your

class Question < ActiveRecord::Base
  attr_accessible :poll_id, :title, :answer_attributes

but in your log file it has

Parameters: {"utf8"=>"✓", "authenticity_token"=>"hk+KuNsTLx3sH9pE7Mf8XETGsmxTsRN4/tWUBn3CIVE=", "poll"=>{"title"=>"Testing 1-2-1-2", "description"=>"Up on the mic", "questions_attributes"=>{"0"=>{"title"=>"Cake or Death?", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"title"=>"Cake", "_destroy"=>"0"}, "1"=>{"title"=>"Death", "_destroy"=>"0"}}}}}, "commit"=>"Create Poll"}

The difference being :answers_attributes, rather than :answer_attributes i.e. answer vs answer*s

This may be causing the data to be thrown away.

Upvotes: 2

Related Questions