为什么会出现这种"未定义的方法`#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey"错误? [英] Why am I getting this "undefined method `#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey" error?

查看:185
本文介绍了为什么会出现这种"未定义的方法`#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的关联:

 类问题<的ActiveRecord :: Base的
  has_and_belongs_to_many:脚注
  has_and_belongs_to_many:图片
  的has_many:字段,:依赖=> :破坏
  的has_many:调查:依赖=> :删除所有
  belongs_to的:输入
  belongs_to的:元
  的has_many:截图
  belongs_to的:标准:触摸=>真正
  belongs_to的:产品:触摸=>真正
  belongs_to的:条件:将class_name => 现场,:触摸=>真正
结束

一流的产品和LT;的ActiveRecord :: Base的
  的has_many:问题:依赖=> :破坏
  的has_many:评论
  has_and_belongs_to_many:订单
  的has_many:竞争者
  的has_many:元素:依赖=> :破坏
  的has_many:田
结束

一流的元素<的ActiveRecord :: Base的
  的has_many:问题
  的has_many:标准
  belongs_to的:产品:触摸=>真正
结束

等级标准<的ActiveRecord :: Base的
  的has_many:问题
  的has_many:标准
  belongs_to的:综述
结束

一流的脚注<的ActiveRecord :: Base的
  belongs_to的:参考:触摸=>真正
  has_and_belongs_to_many:问题
结束
 

那么,为什么我会得到以下?

 来源:/Users/steven/Dropbox/Testivate/app/controllers/questions_controller.rb @行80 QuestionsController#更新时间:

    79:DEF更新
 => 80:binding.pry_remote
    81:@question = Question.find(PARAMS [:ID])
    82:@ question.update_attributes(PARAMS [:提问])
    83:// @ question.update_columns:PRODUCT_ID => @ question.element.product.id
    84:闪光[:通知] =问题已成功更新(#{undo_link(@question)}),如果@ question.save
    85:respond_with @question
    86:结束

[1]撬(#&其中; QuestionsController&GT)> @question = Question.find(PARAMS [:ID])
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| ID |站| DES | ELE |精读| CRE | upda |添加|插件|行为| ITE | PRO | INP |男人| ABO | RES |列弗| COM |遇到|
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1 | 1 |是| 1 | | 201 | 2014年| TRU |关于| FAL | 1 | 1 | | FAL | | | 0 | FAL | FAL |
+----+-----+-----+-----+-----+-----+------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
1行中集
[2]撬(#&其中; QuestionsController&GT)> PARAMS
=> {UTF8=>中✓
 _method=>补丁,
 问题=>
  {说明=>中是不是可读?,
   element_id=>中1,
   standard_id=>中1,
   关于=>中,
   添加剂=>中真,
   迭代=>中1,
   指令=>中在主页上,有:,
   picture_ids=>(),
   footnote_ids=>(),
   有效的=>中0,
   手册=>0},
 提交=>中更新问题,
 行动=>中更新,
 控制器=>中的问题,
 ID=>中1}
[3]撬(#&其中; QuestionsController&GT)> @ question.save
=>真正
[4]撬(#&其中; QuestionsController&GT)> @ question.update_attributes(PARAMS [:提问])
NoMethodError:未定义的方法`#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey:0x0000010b21ce90>'为#&LT;问题:0x0000010c0dda38&GT;
从/Users/steven/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing的'
 

解决方案

变量

一般情况下,你会收到一个未定义的方法错误,试图调用对象的方法,它不存在,或不正确定义:

  @ q​​uestion.update_attributes(PARAMS [:提问])
 

看起来像是做,这是导致该错误


错误

我们之前已经有过这样的问题,它的下到:

<$p$p><$c$c>ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey

由于按照 Rails文档

  

协会代理的活动记录的对象之间的中间人   保存的关联,被称为@owner,与实际   关联的对象,被称为@target。那种协会任   代理约是@reflection可用。这是的一个实例   一流的ActiveRecord ::反思:: AssociationReflection。

这意味着,无论是发生的事情是调用代理对象为你的方法(这基本上是相关联的真实对象)。基本上,你要调用数组(集合)的方法,而不是对象本身

你需要做这样的事情来解决这个问题:

  @ q​​uestion.surveys.first
 


修正

我没有用 前,但我会尝试这样的:

  @ q​​uestion.update(question_params)

私人

高清question_params
    params.require(:问题).permit(:描述:element_id,:standard_id,约,:添加剂:迭代,picture_ids:[],footnote_ids:[]:积极,:手动)
结束
 

I have the following 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 :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

Why, then, do I get the following?

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'

解决方案

Variable

As a general rule, you'd receive a undefined method error by trying to call a method on an object which either doesn't exist, or is incorrectly defined:

@question.update_attributes(params[:question])

Looks like something to do with this is causing the error


Error

We've had problems like this before, and it's down to this:

ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey"

As per the Rails documentation:

Association proxies in Active Record are middlemen between the object that holds the association, known as the @owner, and the actual associated object, known as the @target. The kind of association any proxy is about is available in @reflection. That's an instance of the class ActiveRecord::Reflection::AssociationReflection.

What this means is whatever is happening is calling the proxy object (which basically associates the real objects) in to your methods. Basically you're trying to call methods on an array (collection), rather than the object itself

You'd need to do something like this to fix it:

@question.surveys.first


Fix

I've not used pry before, but I'd try this:

@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

这篇关于为什么会出现这种&QUOT;未定义的方法`#&lt;ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey&quot;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆