嵌套属性保存错误

时间:2011-03-27 13:05:54

标签: ruby-on-rails ruby-on-rails-3

我有模特

class Survey < ActiveRecord::Base
  has_many :questions
  acts_as_list

  validates :title, :presence =>true
  validates :short_description, :presence=>true
  validates :description, :presence=>true

end


class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :options

  accepts_nested_attributes_for :options, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true

  acts_as_list :scope=>:survey
end

class Option < ActiveRecord::Base
  attr_accessible :title, :description, :position
  belongs_to :question

  acts_as_list :scope=>:survey
end

当我保存或更新问题模型时,它会生成错误

@question        = Question.new(params[:question])
@question.save
@question        = Question.find(params[:id])
@question.update_attributes(params[:question])

在这两种情况下都会产生错误

NoMethodError (undefined method `survey_id' for #<Option:0xb332394>):
  app/controllers/admin/questions_controller.rb:47:in `block in create'
  app/controllers/admin/questions_controller.rb:46:in `create'

我无法理解为什么会产生此错误,因为Option与Survey

没有任何关系

1 个答案:

答案 0 :(得分:0)

可能是这个(在选项上):

class Option < ActiveRecord::Base
  attr_accessible :title, :description, :position
  belongs_to :question

  acts_as_list :scope=>:survey   # <-- no survey_id ??
end