anastasiaalt / quiz

Anastasia's Ruby on Rails Project
1 stars 0 forks source link

Undefined Method Destroy_All for Quiz Module #1

Closed anastasiaalt closed 8 years ago

anastasiaalt commented 8 years ago

I am attempting to seed my database. After creating a database, generating migrations, and migrating the database, my next step is to seed the database. When I run rake db:seed from the command line, I get this error: https://github.com/anastasiaalt/quiz/blob/master/db/seeds.rb

- $ rake db:seed
- rake aborted!
- NoMethodError: undefined method `destroy_all' for Quiz:Module

Based on this error, I have been investigating my Quiz module, which looks like the below at present: https://github.com/anastasiaalt/quiz/blob/master/app/models/quiz.rb

class Quiz < ActiveRecord::Base
  belongs_to :instructor

  has_many :students
  has_many :questions

  has_many :answers, through: :questions
  has_many :options, through: :questions
end

My suspicion is the error I am getting has something to do with the way I am connecting answers and options to Quiz.

I have searched for answers on Stack Overflow and the Ruby on Rails docs for answers related to this error and have not found anything conclusive. One article pointed in the direction of models inheriting from one another rather than from ActiveRecord, e.g. Option inherits from Question or Answer inherits from Question but I did not understand when to possibly use this versus has_many: with through:

My first attempt did not have these two lines in the model but added them after research and in both instances I get the same error

  has_many :answers, through: :questions
  has_many :options, through: :questions

If I comment out this line in the seeds file

Quiz.destroy_all

I get this error when I attempt rake db:seed from the command line

$ rake db:seed
rake aborted!
NoMethodError: undefined method `password_digest=' for #<Student:0x007fdb23f67ec0>
/Users/anastasiaalt/quizProject/quiz/db/seeds.rb:30:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

I recognize this is a separate error connected to passwords and am still troubleshooting. I will file a separate issue if I cannot work past it.

short-matthew-f commented 8 years ago

Anastasia: The reason you're getting this error is because there is a module named after your application. So when you did rails new quiz, it created a module named Quiz.

Later, you created a model named Quiz, which means there is also a class named Quiz. So now there are two things happening here, both with the same name. When you call Quiz.destroy_all it finds the nearest thing called Quiz, which is the module. OH NO!

So, either rename your app, or rename the model. I suggest the model.

  1. rake db:drop
  2. Find the file quiz.rb and rename it assessment.rb, then change class Quiz to class Assessment in that file.
  3. Find any reference to Quiz that's not module Quiz, and replace it with Assessment
  4. any has_many :quizzes should be has_many :assessments, and belongs_to :quiz should be belongs_to :assessment, etc.
  5. Find your migration file for Quiz, and change quiz to assessment.
Avizacherman commented 8 years ago

Anastasia,

The password relates to the specific name of the password_digest field required by using has_secure_password in your models. Your instructor schema shows you called the field encrypted_password but it must be called password_digest. Same thing with your student model and schema. This is just how Rails expects things to be done.

As for the destroy_all method coming back undefined, there's actually two separate issues here. The first, and more important is that the Rails application itself is called Quiz, so you can't have anything else called Quiz. You will need to rename this table to something else. As a secondary, the plural of quiz is quizzes with 2 "z"s. Just be sure that you are spelling pluralized forms correctly. If necessary, use the rails generators for models just to get yourself started and then add the fields in in a later migration.

Hope that helps.

-Avi