ruby-on-rails - 为什么我会收到这个 "undefined method ` #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey”错误?

标签 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 :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'

最佳答案

变量

作为一般规则,您会收到 undefined method尝试在不存在或定义不正确的对象上调用方法时出错:

@question.update_attributes(params[:question])

看起来与此有关导致错误

错误

我们以前遇到过这样的问题,归结为:
ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey”

根据 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.



这意味着正在发生的任何事情都在调用 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

关于ruby-on-rails - 为什么我会收到这个 "undefined method ` #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22062155/

相关文章:

ruby-on-rails - Rails 6.1,Ruby 3.0.0 : tests error as they cannot load rexml

ruby-on-rails - Ruby on Rails : 2. 3.8,过滤器不起作用之前的自定义?

ruby-on-rails - 英雄库 : run rake db:migrate error

Ruby 类关联

ruby-on-rails - Rails Mailer 使用 SendGrid 模板

ruby-on-rails - activerecord如何查询关联的多个条件?

android - Android的更高级别的数据库层?

ruby-on-rails - Rails 5 从两个不同的表中选择并得到一个结果

mysql - Sequelize — 通过关联对象的精确数组查找?

ruby-on-rails-3 - Rails - 如何使用重新加载方法?