gpt4 book ai didi

ruby-on-rails -
转载 作者:行者123 更新时间:2023-12-03 10:32:30 27 4
gpt4 key购买 nike

我收到错误消息:

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

错误在代码段的第三行。
  def artist
@records = Record.where('artist_id = ' + params[:artist_id])
render :json => @records, serializer: RecordSummarySerializer
end

这是 Controller RecordsContrller的备用序列化器。另一个是'RecordsSerializer,效果很好。

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

到发生错误的类。它对我不起作用。我还浏览了gem文档。我找到了使用序列化程序的示例:很明显,还需要其他任何代码。

我正在使用Ruby 2.3.0和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中看到了它。

更新资料

我添加了@JMazzy建议的代码。我现在得到错误:
<NoMethodError: undefined method `id' for #<Record::ActiveRecord_Relation:0x007faa8005a9d8>\nDid you mean?  ids>

最佳答案

我遇到了同样的错误,发现在 Controller 中将serializer:更改为each_serializer:解决了该问题。

Controller

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 documentation

更新的链接。感谢Lowryder

关于ruby-on-rails - <NoMethodError:#<Record::ActiveRecord_Relation的未定义方法 `read_attribute_for_serialization':,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510327/

27 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com