- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误消息:
<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
alias :read_attribute_for_serialization :send
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
include ActiveModel::Serialization
class RecordSummarySerializer < ActiveModel::Serializer
attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,
:recording_date, :penguin, :category
end
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
<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
class RecordSummarySerializer < ActiveModel::Serializer
attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,
:recording_date, :penguin, :category
end
关于ruby-on-rails - <NoMethodError:#<Record::ActiveRecord_Relation的未定义方法 `read_attribute_for_serialization':,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510327/
我有以下查询: User.joins(:posts).group('users.id').having('count(user_id) > 1') 返回的类是User::ActiveRecord_Re
拥有: class Foo < ApplicationRecord def self.to_csv # irrelevant end end Rails 允许我做: Foo.all.t
我有一个users 表、一个notifications 表和一个notification_reads 连接表。我正在尝试为 ActiveRecord 查询编写一个范围,该查询将返回所有通知 (Noti
基本上,我正在尝试设置 Trainer 将注册的日历,如下所示: @calendar = Calendar.where("current = 1") @trainer.calendar = @cale
希望这不是一个重复的问题,因为我花了数周时间在 StackOverflow 上搜索答案,但没有找到解决我问题的方法。 我有一个 Ruby on Rails API(Ruby 1.9.3,Rails 4
我正在从 Rails 3 更新到 Rails 4.2,当我运行应用程序的测试套件时,出现以下错误: Failure/Error: get 'edit', id: @shops[3].id NoMe
如何从 Rails 中的 ActiveRecord_Relation 中删除最后一个元素? 例如如果我设置: @drivers = Driver.all 我可以通过执行以下操作将另一个名为 @new_
对不起,愚蠢的问题 在这里养的时候@property我正在排队,但是当我提高时 @property.id它正在显示 undefined method id给我结论 在 owner.rb has_man
Rails 新手,正在尝试使用 Google Maps API。我希望循环遍历并显示数据库中每个地理位置的标记。当我在窗口中打开控制台并查找变量 [places](该变量应具有我所有 @locatio
我正在尝试显示按名字或姓氏搜索的用户的数据。我能够显示为用户返回的所有数据(例如,如果按名字搜索用户,我可以提取与搜索到的用户关联的所有其他字段/数据,例如城市、州/省等)。 似乎给我一个错误的唯一字
我有三个模型。 User、Message 和Filter,关系如下图。如何选择 User.find(*).filters.messages? 用户: has_many :filters has_and
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我有一个国家模型和一个游记模型。一个国家有很多游记,一篇游记属于一个国家。 在 Rails 控制台中: TravelNote.published.country(248) [#] TravelNot
我正在使用 select in with通过find_by_sql查询: scope :error_percentage, -> (user_obj) { find_by_sql("WITH pr
我有两个事件记录数组。我合并了这两个数组,现在我想根据“updated_at”字段对它们进行排序。 @notification_challenges=Challenge.where("to_id=?
我正在尝试产品数量 - 1 但我收到此错误 line_item.rb belongs_to :order belongs_to :product 支付.rb has_many :orders #Lin
我今天有一个不典型的问题。我有阻塞的条件: User.where do |user| user if Avatar.where(id: user.avatar_id).empty? end.fir
我通过构建应用程序来学习 Rails。在我将此代码添加到我的 users_controller.rb 之前,我的应用程序运行良好 def show @user = User.find(para
我遇到了未定义方法 `to_key' 的问题 这是我的 books_controller.rb class BooksController ... ... 现在当我要访问索引页
我在 form_for 渲染方面遇到了问题。我想要一个 _form.html.erb 部分来处理我的问题记录的创建和编辑。当我沿着使用路线走时 我收到以下错误: undefined method `
我是一名优秀的程序员,十分优秀!