<NoMethodError: #<Record::ActiveRecord_Relation 的未定义方法`read_attribute_for_serialization': [英] <NoMethodError: undefined method `read_attribute_for_serialization' for #<Record::ActiveRecord_Relation:

查看:25
本文介绍了<NoMethodError: #<Record::ActiveRecord_Relation 的未定义方法`read_attribute_for_serialization':的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:

<NoMethodError: undefined method `read_attribute_for_serialization' for #<Record::ActiveRecord_Relation:0x007faa7cfe3318>>

错误在代码段的第三行.

The error is on third line of the snippet.

  def artist
    @records = Record.where('artist_id = ' + params[:artist_id])
    render :json => @records, serializer: RecordSummarySerializer
  end

这是控制器 RecordsContrller 的替代序列化程序.另一个,'RecordsSerializer,工作正常.

This is an alternate serializer for the controller RecordsContrller. The other one, 'RecordsSerializer, works fine.

对此错误的搜索提出了一些解决方案.有些人不清楚在哪里添加代码.另一个建议添加:

A search on this error brought up a few solutions. Some were unclear as to where one would add code. Another suggested adding:

alias :read_attribute_for_serialization :send

到发生错误的类.它对我不起作用.我还查看了 gem 文档.我找到了一个使用序列化程序的例子:.很明显还需要任何其他代码.

to the class where the error occurs. It didn't work for me. I also looked through the gem documentation. I found an example of the use of serializer:. It was clear any other code was needed.

我使用的是 Ruby 2.3.0 和 Rails 5.0.0.

I'm using Ruby 2.3.0 and Rails 5.0.0.

class RecordsController < ApplicationController
...
  def index
    @records = Record.order('title')
    render :json => @records
  end

  def artist
    @records = Record.where('artist_id = ' + params[:artist_id])
    render :json => @records, serializer: RecordSummarySerializer
  end
...
end

型号

class Record < ActiveRecord::Base
  belongs_to :artist
  belongs_to :label
  belongs_to :style
  has_many :tracks
  has_many :credits

  validates :title, presence: true
  validates :catalog, presence: true
end

record_summary_serializer.rb

include ActiveModel::Serialization
class RecordSummarySerializer < ActiveModel::Serializer
  attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,
             :recording_date, :penguin, :category
end

record_serializer.rb

class RecordSerializer < ActiveModel::Serializer
  attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,     :alternate_catalog,
         :recording_date, :notes, :penguin, :category
  has_one :artist
  has_many :tracks
end

记录架构

  create_table "records", unsigned: true, force: :cascade, options:   "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "title",             limit: 50
    t.string   "catalog",           limit: 20,                          null: false
    t.string   "alternate_catalog", limit: 20
    t.integer  "artist_id",                                                          unsigned: true
    t.integer  "label_id",                                                null: false, unsigned: true
    t.integer  "style_id",                                                           unsigned: true
    t.datetime "recording_date"
    t.text     "notes",             limit: 4294967295
    t.string   "penguin",           limit: 50
    t.string   "category",          limit: 50,         default: "jazz"
    t.index ["artist_id"], name: "RecordHasOneArtist", using: :btree
    t.index ["label_id"], name: "RecordHasOneLabel", using: :btree
    t.index ["style_id"], name: "RecordHasOneStyle", using: :btree
  end

我刚刚注意到,主键 id 没有出现在架构中.我在Sequel Pro中查看表结构时看到的.

I just noticed, the primary key, id, doesn't appear in the schema. I see it in Sequel Pro when I view the table structure.

我添加了@JMazzy 建议的代码.我现在收到错误:

I added the the code suggested by @JMazzy. I now get the error:

<NoMethodError: undefined method `id' for #<Record::ActiveRecord_Relation:0x007faa8005a9d8>\nDid you mean?  ids>

推荐答案

我遇到了同样的错误,发现在控制器中将 serializer: 更改为 each_serializer: 解决了问题.

I had the same error and found changing serializer: to each_serializer: in the controller solved the issue.

控制器

class RecordsController < ApplicationController
...
  def artist
    @records = Record.where('artist_id = ' + params[:artist_id])
    render :json => @records, each_serializer: RecordSummarySerializer
  end
...
end

record_summary_serializer.rb - 可以删除包含行

class RecordSummarySerializer < ActiveModel::Serializer
  attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,
             :recording_date, :penguin, :category
end

来自 active_model_serializers 文档.

From active_model_serializers documentation.

更新了链接.感谢洛瑞德

Updated link. thanks Lowryder

这篇关于&lt;NoMethodError: #&lt;Record::ActiveRecord_Relation 的未定义方法`read_attribute_for_serialization':的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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