【问题标题】:Rails 3.1: Trying to update ActiveRecord: NoMethodError: undefined method `keys' for #<ActiveRecord::Relation:0x007fdfb82aca98>Rails 3.1: 尝试更新 ActiveRecord: NoMethodError: undefined method `keys' for #<ActiveRecord::Relation:0x007fdfb82aca98>
【发布时间】:2011-11-16 00:36:47
【问题描述】:

我正在尝试更新 ActiveRecord,这看起来应该很容易。我尝试过的方法

控制器 (metadata_controller.rb)

  def update_metadata_type
    @metadata_type = MetadataType.find(params[:id])
    if @metadata_type.update_attributes(params)
      render :template => 'metadata/show_metadata_type'
    else
      # return error template
    end
  end

  def update_metadata_type
    if MetadataType.update(1, { :name => params[:name] })
      render :template => 'metadata/show_metadata_type'
    else
      # return error template
    end
  end

  def update_metadata_type
    @metadata_type = MetadataType.find(params[:id])
    @metadata_type.name = params[:name]
    @metadata_type.save

    render :template => 'metadata/show_metadata_type'
  end


模型(metadata_type.rb):

class MetadataType < ActiveRecord::Base
  has_many :metadata_type_attributes, :dependent => :destroy
  has_many :attributes, :through => :metadata_type_attributes, :dependent => :destroy

  attr_accessible :name, :attribute_type_id

  validates :name, :uniqueness => true
end


具有相同问题的不同模型(attribute_type.rb):

class AttributeType < ActiveRecord::Base
  belongs_to :attributes

  attr_accessible :data_type

  validates :data_type, :uniqueness => true
end


routes.rb:

  match '/metadata_type/:id' => 'metadata#update_metadata_type', :via => [:put], :as => 'update_metadata_type'

每次我使用 RESTClient 发送数据时,都会使用适当的标头发送 json,并在 PUT 命令中包含以下数据:

{
    "name" : "NewName"
}

我还在控制器中使用了 binding.pry 来确保找到 @metadata_type(而不是 nil)并且它总是正确的:

[1] pry(#<MetadataController>)> @metadata_type = MetadataType.find(params[:id])
=> #<MetadataType id: 1, name: "Category", created_at: "2011-11-15 16:02:53", updated_at: "2011-11-15 16:02:53">

参数看起来也不错:

[7] pry(#<MetadataController>)> params
=> {"name"=>"NewName",
 "controller"=>"metadata",
 "action"=>"update_metadata_type",
 "id"=>"1",
 "metadatum"=>{"name"=>"NewName"}}

但由于某种原因,无论我如何尝试保存,我都会收到相同的错误:

NoMethodError (undefined method `keys' for #<ActiveRecord::Relation:0x007fdfb7637fd8>):
  app/controllers/metadata_controller.rb:50:in `update_metadata_type'

知道是什么原因造成的吗?感谢所有帮助

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.1


    【解决方案1】:

    抱歉,我来晚了,但您无法定义与名称 attributes 的关联,因为它最终会覆盖 ActiveRecord 本身定义和使用的内置 attributes 访问器方法。

    希望这对碰巧遇到这个晦涩错误的其他人有所帮助。

    【讨论】:

      【解决方案2】:

      问题在于这些行:

      has_many :metadata_type_attributes, :dependent => :destroy
      has_many :attributes, :through => :metadata_type_attributes, :dependent => :destroy
      

      我对你到底想做什么有点困惑,但我怀疑你的意思是:

      has_many :attributes, :class_name => "MetadataTypeAttributes", :dependent => :destroy
      

      【讨论】:

      • 好像不是这样。我尝试了您的编辑,现在却收到此错误:NameError: uninitialized constant MetadataType::MetadataTypeAttributes from /Users/dsavage/ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1341:in compute_type'。我还在上面进行了编辑并添加了一个不同的模型,我正在尝试做同样的事情,但遇到了同样的问题。
      • 我对你的模型设置有点困惑。为什么 Metadata 有很多 Attributes 和 MetadataTypeAttributes。两者都有型号吗?如果可能,发布相关模型和模型架构的一些说明。
      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2011-09-05
      • 1970-01-01
      • 2020-09-24
      相关资源
      最近更新 更多