3

我一直尝试在谷歌上搜索这个错误,但无济于事。我目前有这些模型

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

应用程序/模型/sub_survey.rb

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

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

应用程序/模型/choice.rb

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

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

现在,当我尝试销毁调查时,出现错误

uninitialized constant Question::Choice

在survey.destroy之后通过/vendor/rails/active*的东西追踪

然后,当我尝试从 question.Choices 访问选项时,出现错误

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

由于某种原因,它在跟踪堆栈的顶部

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'

在以 xml 格式导入调查时,我确实使用了 attribute_fu,但我不知道为什么 question.Choices 的痕迹有它。

我还尝试将选项重命名为选择选项,但这没有效果。

有任何想法吗?

4

2 回答 2

4

您的Choices表已经有一个复数名称,这可能会导致问题。理想情况下,应该调用该表,Choice否则您has_many :choices也应该指定该class_name选项。例如

has_many :choices, :class_name => 'Choices'

Choice虽然如果可以的话,我会选择重命名类和表。

Attachment_fu 可能出现在堆栈跟踪中,因为它们已覆盖或为该method_missing方法添加别名以添加自己的行为。这不一定是值得关注的事情。

于 2009-07-31T13:36:08.973 回答
0

我不确定为什么您在销毁调查时会收到错误消息,但您会收到此消息

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

因为您应该像这样访问它:

question.choices # No capitalization

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

于 2009-07-31T13:36:02.233 回答