Jonathan Musso
Jonathan Musso

Reputation: 1374

Unable to Destroy record. No association found

I am a new student learning about Ruby on Rails. For my current assignment I have to build a Question feature. Everything works but the deletion of a "Question".

I am trying to follow the Form Helper One to Many. Thanks for looking.

Instructions

Complete QuestionsController and its corresponding views. Accept input for resolved in the Question form using a checkbox.

Test your changes in the browser. Confirm that you can:

see an index of all questions, view an individual question, create new questions, edit and update questions, and delete questions.

When I go to edit a question and then proceed to delete it with the checkbox this error is thrown:

No association found for name `question'. Has it been defined yet?

Here is my console output:

Started GET "/questions/1/edit" for ::1 at 2015-04-23 15:42:11 -0400
Processing by QuestionsController#edit as HTML
Parameters: {"id"=>"1"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

ArgumentError (No association found for name `question'. Has it been defined yet?):
app/models/question.rb:3:in `<class:Question>'
app/models/question.rb:1:in `<top (required)>'
app/controllers/questions_controller.rb:27:in `edit'

questions_controller.rb

class QuestionsController < ApplicationController
  def index
    @questions = Question.all
  end

  def show
    @question = Question.find(params[:id])
  end

  def create
    @question = Question.new(params.require(:question).permit(:title, :body, :resolved))

    if @question.save
      flash[:notice] = "Question was saved."
      redirect_to @question
    else
      flash[:error] = "There was an error saving the question. Please try again."
      render :new
    end
  end

  def new
    @question = Question.new
  end

  def edit
    @question = Question.find(params[:id])
  end

  def update
    @question = Question.find(params[:id])
    if     @question.update_attributes(params.require(:question).permit(:title, :body, :resolved, :_destroy))
      flash[:noteice] = "Question was updated."
      redirect_to @question
    else
      flash[:error] = "There was an error saving the question. Please try again."
      render :edit
    end
  end
end

question.rb

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :question, allow_destroy: true
end

edit.html.erb

<div class="col-md-8">
<%= form_for @question do |f| %>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control', placeholder: "Enter question title" %>
  </div>
  <div class="form-group">
    <%= f.label :body %>
    <%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter question body" %>
  </div>
  <div class="form-group">
    <%= f.label :resolved %>
    <%= f.check_box :resolved %>
  </div>
  <div class="form-group">
    <%= f.label :destroy %>
    <%= f.check_box :_destroy %>
  </div>
  <div class="form-group">
    <%= f.submit "Save", class: 'btn btn-success' %>
  </div>
<% end %>

Edit: added my answers.rb.

answers.rb

class Answer < ActiveRecord::Base
  belongs_to :question
end

Upvotes: 2

Views: 217

Answers (3)

user229044
user229044

Reputation: 239240

Nested attributes are for the relation. Your Question has many Answers. The point of accepts_nested_attributes_for is that it lets you update "nested" associations from the parent model. The argument is the name of the association to accept attributes for. In this case, your questions should accept nested attributes for their answers:

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :answers
end

See the documentation, where the example class Book accepts nested attributes for author and pages. A Book does not accept nested attributes for a book, similarly your question should not accept nested attributes for a question.


As far as deleting the question, you cannot use accepts_nested_attributes to point a model back to itself so that _destroy will destroy the parent model.

You'll have to check for the presence of that attribute and destroy the record in your controller.

Upvotes: 3

Paul Richter
Paul Richter

Reputation: 11072

The problem is the accepts_nested_attributes_for line in the Question class.

You wrote

accepts_nested_attributes_for :question

but it likely should be

accepts_nested_attributes_for :answers

since that is the name of the has_many relationship. Rails is barking because there is no such relationship called :question inside the Question class. Your class should look like this:

class Question < ActiveRecord::Base
  has_many :answers
  accepts_nested_attributes_for :answers, allow_destroy: true
end

Upvotes: 1

CoolTapes
CoolTapes

Reputation: 417

You need in answers.rb

belongs_to :question

in order to complete the association

Upvotes: -1

Related Questions