通过关联访问未定义的方法,在尝试使用:dependent =>:destroy时未初始化的常量

时间:2022-10-17 11:15:20

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

我一直在谷歌上搜索这个错误,但没有结果。我现在有这些模型

app/models/survey.rb

应用程序/模型/ 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

应用程序/模型/ sub_survey.rb

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

app/models/question.rb

应用程序/模型/ 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

应用程序/模型/ choice.rb

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

app/models/answer.rb

应用程序/模型/ 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

在调查后跟踪/供应商/rails/活动*内容

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.

在导入xml格式的调查时,我确实使用了attribute_fu,但我不知道为什么会出现问题。选择它。

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

我还尝试重新命名选择,以选择替代方案,但没有效果。

Any ideas?

什么好主意吗?

2 个解决方案

#1


4  

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.

您的选择表已经有一个多元化的名称,可能会导致问题。理想情况下,表应该被称为Choice,否则has_many:choices应该也指定class_name选项。如。

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.

Attachment_fu可能出现在堆栈跟踪中,因为它们已经覆盖或别名了method_missing方法来添加它们自己的行为。这并不一定要担心。

#2


0  

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.

我认为这应该能解决其中一个问题。

#1


4  

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.

您的选择表已经有一个多元化的名称,可能会导致问题。理想情况下,表应该被称为Choice,否则has_many:choices应该也指定class_name选项。如。

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.

Attachment_fu可能出现在堆栈跟踪中,因为它们已经覆盖或别名了method_missing方法来添加它们自己的行为。这并不一定要担心。

#2


0  

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.

我认为这应该能解决其中一个问题。