为什么会出现“undefined method `#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey” 错误?

6
我有以下的关联项:
class Question < ActiveRecord::Base
  has_and_belongs_to_many :footnotes
  has_and_belongs_to_many :pictures
  has_many :fields, :dependent => :destroy
  has_many :surveys, :dependent => :delete_all
  belongs_to :input
  belongs_to :element
  has_many :screenshots
  belongs_to :standard, :touch => true
  belongs_to :product, :touch => true
  belongs_to :condition, :class_name => "Field", :touch => true
end

class Product < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  has_many :reviews
  has_and_belongs_to_many :orders
  has_many :competitors
  has_many :elements, :dependent => :destroy
  has_many :fields
end

class Element < ActiveRecord::Base
  has_many :questions
  has_many :standards
  belongs_to :product, :touch => true
end

class Standard < ActiveRecord::Base
  has_many :questions
  has_many :standards
  belongs_to :review
end

class Footnote < ActiveRecord::Base
  belongs_to :reference, :touch => true
  has_and_belongs_to_many :questions
end

那么,我为什么会得到以下结果?
From: /Users/steven/Dropbox/Testivate/app/controllers/questions_controller.rb @ line 80 QuestionsController#update:

    79: def update
 => 80:   binding.pry_remote
    81:   @question = Question.find(params[:id])
    82:   @question.update_attributes(params[:question])
    83:   @question.update_columns :product_id => @question.element.product.id
    84:   flash[:notice] = "Question was successfully updated. (#{undo_link(@question)}.)" if @question.save
    85:   respond_with @question
    86: end

[1] pry(#<QuestionsController>)> @question = Question.find(params[:id])
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| id | sta | des | ele | con | cre | upda | add | ins | act | ite | pro | inp | man | abo | res | lev | com | met |
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1  | 1   | Is  | 1   |     | 201 | 2014 | tru | On  | fal | 1   | 1   |     | fal |     |     | 0   | fal | fal |
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
1 row in set
[2] pry(#<QuestionsController>)> params
=> {"utf8"=>"✓",
 "_method"=>"patch",
 "question"=>
  {"description"=>"Is it readable?",
   "element_id"=>"1",
   "standard_id"=>"1",
   "about"=>"",
   "additive"=>"true",
   "iterations"=>"1",
   "instructions"=>"On the homepage, there is:",
   "picture_ids"=>[""],
   "footnote_ids"=>[""],
   "active"=>"0",
   "manual"=>"0"},
 "commit"=>"Update Question",
 "action"=>"update",
 "controller"=>"questions",
 "id"=>"1"}
[3] pry(#<QuestionsController>)> @question.save
=> true
[4] pry(#<QuestionsController>)> @question.update_attributes(params[:question])
NoMethodError: undefined method `#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey:0x0000010b21ce90>' for #<Question:0x0000010c0dda38>
from /Users/steven/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'

1
抱歉离题了,但是你是如何让Pry以表格形式显示数据库结果的? - Daryll Santos
1
我猜那是 Hirb:https://github.com/cldwalker/hirb - steven_noble
1个回答

3

变量

通常情况下,如果您尝试在对象上调用一个不存在或定义不正确的方法,则会收到undefined method错误:

@question.update_attributes(params[:question])

似乎与此有关的某些事情导致了错误。

错误

我们之前遇到过类似的问题,而且问题出在这里:

ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey

根据Rails documentation,Active Record中的关联代理是持有关联(称为@owner)和实际关联对象(称为@target)之间的中间人。代理类型可在@reflection中获得。这是一个ActiveRecord :: Reflection :: AssociationReflection类的实例。
换句话说,无论发生什么事情,都会将“proxy”对象(基本上将真正的对象关联起来)调用到您的方法中。 基本上,您正在尝试在数组(集合)上调用方法,而不是对象本身。
要修复它,您需要执行以下操作:
@question.surveys.first

修复

我以前没有使用过pry,但我会尝试这样做:

@question.update(question_params)

private

def question_params
    params.require(:question).permit(:description, :element_id, :standard_id, :about, :additive, :iterations, picture_ids: [], footnote_ids: [], :active, :manual)
end

谢谢。虽然我最终没有采用你的解决方案,但是你提到@question.surveys.first让我意识到“Proxy_Survey”是指我的“has_many:surveys”关系。这促使我调查了与该关系有关的@question回调函数。而那就是我找到错误的地方。谢谢。 - steven_noble

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接