views:

665

answers:

2

Hello. I've tried persistently googling this error, but to no avail. I currently have these models

app/models/survey.rb

class Survey < ActiveRecord::Base
  belongs_to :user
  has_attached_file :original, :default_url => "/public/:class/:attachment/:basename.:extension"
  has_many :sub_surveys, :dependent => :destroy
end

app/models/sub_survey.rb

class SubSurvey < ActiveRecord::Base
  belongs_to :survey
  has_many :questions, :dependent => :destroy
end

app/models/question.rb

class Question < ActiveRecord::Base
  belongs_to :sub_survey
  validates_presence_of :sub_survey
  acts_as_list :scope => :sub_survey
  #after_destroy :destroy_orphaned_choices

  has_many :answers, :dependent => :destroy
  has_many :choices, :dependent => :destroy
end

app/models/choice.rb

class Choices < ActiveRecord::Base
  belongs_to :question
  validates_presence_of :question
end

app/models/answer.rb

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
  belongs_to :game
  validates_uniqueness_of :question_id, :scope => [:user_id, :game_id]
end

Now when I try to destroy a survey, I get an error

uninitialized constant Question::Choice

That traces through /vendor/rails/active* stuff after the survey.destroy

Then when I try to access choices from question.Choices, I get an error

undefined method `Choices' for #<Question:0xb7224f2c>

which for some reason has this on top of the trace-stack

vendor/rails/activerecord/lib/active_record/attribute_methods.rb:256:in `method_missing'
vendor/plugins/attribute_fu/lib/attribute_fu/associations.rb:28:in `method_missing'
app/views/answers/_answer.html.erb:7:in `_run_erb_47app47views47answers47_answer46html46erb'

I do use attribute_fu when importing surveys in xml-format, but I have no idea why the trace of question.Choices has it.

I also tried renaming choices to choicealternatives, but that didn't have an effect.

Any ideas?

A: 

I'm not sure why you get the error when destroying a Survey, but you're getting this

undefined method `Choices' for #<Question:0xb7224f2c>

because you should be accessing it like this:

question.choices # No capitalization

I think that should solve one of the problems.

Mike Trpcic
Worked! Thank you. Set the error to the same, which the comment below fixed.
A: 

Your Choices table has already got a pluralised name which may be causing problems. Ideally that table should be called Choice otherwise your has_many :choices should specify the class_name option too. E.g.

has_many :choices, :class_name => 'Choices'

Though I'd opt for renaming the class and table Choice if you can.

Attachment_fu is probably appearing in the stack trace because they have overridden or aliased the method_missing method to add their own behaviour. It's not necessarily anything to be concerned about.

Shadwell
Ah this worked! Thank you very much! I'll try to keep this in mind.