我有以下关联:
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'
答案 0 :(得分:3)
<强>可变强>
作为一般规则,您尝试在对象上调用方法时会收到undefined method
错误,该对象不存在或未正确定义:
@question.update_attributes(params[:question])
看起来与此有关导致错误
错误强>
我们之前遇到过这样的问题,而且还有这个问题:
ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey”
Active Record中的关联代理是对象之间的中间人 持有该关联,称为@owner,以及实际 关联对象,称为@target。任何一种关联 代理是关于在@reflection中可用的。这是一个例子 class 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