Ruby on rails 为什么我会得到这个;未定义的方法`#<;ActiveRecord::Associations::CollectionProxy::ActiveRecord“协会”和“CollectionProxy”调查;错误?
我有以下协会:Ruby on rails 为什么我会得到这个;未定义的方法`#<;ActiveRecord::Associations::CollectionProxy::ActiveRecord“协会”和“CollectionProxy”调查;错误?,ruby-on-rails,activerecord,associations,Ruby On Rails,Activerecord,Associations,我有以下协会: 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 :
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
类问题:destroy
有多个:调查,:相关=>:全部删除
属于:输入
属于:元素
有很多:截图
属于:标准,:touch=>true
属于:产品,:touch=>true
属于:条件,:class\u name=>“字段”,:touch=>true
结束
类产品:破坏
有很多评论吗
有很多订单吗
你有很多竞争者吗
有多个:元素,:dependent=>:destroy
你有很多领域吗
结束
class元素true
结束
类标准true
有很多问题吗
结束
那么,为什么我会得到以下信息
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'
From:/Users/steven/Dropbox/Testivate/app/controllers/questions_controller.rb@line 80 questions controller#更新:
79:def更新
=>80:binding.pry_remote
81:@question=question.find(参数[:id])
82:@question.update_属性(参数[:question])
83:@question.update\u列:product\u id=>@question.element.product.id
84:flash[:notice]=“问题已成功更新。{undo_链接(@Question)}.”如果@Question.save
85:用@question回答_
86:完
[1] pry(#)>@question=question.find(参数[:id])
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|我的名字是“站”在“站”上,“站”在“站”上,“站”上,“站”在“站”上,“站”上,“站”上,“站”上,“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”上“站”|
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|1 | 1 | Is | 1 | 201 | 2014 | tru | On | fal | 1 | fal | 0 | fal | fal|
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
一排
[2] 撬动(#)>参数
=>{“utf8”=>“✓",
“_方法”=>“补丁”,
“问题”=>
{“description”=>“它可读吗?”,
“元素id”=>“1”,
“标准_id”=>“1”,
“关于”=>”,
“加法”=>“真”,
“迭代次数”=>“1”,
“说明”=>“在主页上,有:”,
“图片ID”=>[“”],
“footnote_id”=>[“”],
“活动”=>“0”,
“手动”=>“0”},
“提交”=>“更新问题”,
“操作”=>“更新”,
“控制器”=>“问题”,
“id”=>“1”}
[3] pry(#)>@question.save
=>正确
[4] pry(#)>@question.update_属性(参数[:question])
NoMethodError:未定义的#
来自/Users/steven/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active\u model/attribute\u methods.rb:439:in'method\u missing'
变量
一般来说,如果试图调用不存在或定义不正确的对象上的方法,则会收到未定义方法错误:
@question.update_attributes(params[:question])
看来是与此有关的原因导致了错误
错误
我们以前也遇到过类似的问题,原因是:
ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey”
根据:
活动记录中的关联代理是对象之间的中间人
它包含被称为@owner的关联和实际的
关联对象,称为@target。任何关联的类型
代理大约在@reflection中可用。这是
类ActiveRecord::Reflection::AssociationReflection
这意味着无论发生什么,都要调用代理
对象(它基本上与真实对象关联)到您的方法中。基本上,您试图调用数组(集合)上的方法,而不是对象本身
您需要执行以下操作来修复它:
@question.surveys.first
修复
我以前没用过,但我想试试这个:
@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
很抱歉离题了,但您是如何让Pry在表中显示数据库结果的?我想这是Hirb:谢谢。虽然我没有最终实现您的解决方案,但您提到的@question.surveys这个事实。首先让我意识到“Proxy\u Survey”是指我的“has\u many:surveys”关系。这让我调查了cal我回到了@question这个关系。这就是我发现bug的地方。谢谢。