ruby on rails - <NoMethodError: undefined method `read_attribute_for_serialization' for #<Record::ActiveRecord_Relation: -


i'm getting error:

<nomethoderror: undefined method `read_attribute_for_serialization' #<record::activerecord_relation:0x007faa7cfe3318>> 

the error on third line of snippet.

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

this alternate serializer controller recordscontrller. other one, 'recordsserializer, works fine.

a search on error brought few solutions. unclear 1 add code. suggested adding:

alias :read_attribute_for_serialization :send 

to class error occurs. didn't work me. looked through gem documentation. found example of use of serializer:. clear other code needed.

i'm using ruby 2.3.0 , rails 5.0.0.

controller

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 

model

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 

schema records

  create_table "records", unsigned: true, force: :cascade, options:   "engine=innodb default charset=utf8" |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 

i noticed, primary key, id, doesn't appear in schema. see in sequel pro when view table structure.

update

i added the code suggested @jmazzy. error:

<nomethoderror: undefined method `id' #<record::activerecord_relation:0x007faa8005a9d8>\ndid mean?  ids> 

i had same error , found changing serializer: each_serializer: in controller solved issue.

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 - can remove include line

class recordsummaryserializer < activemodel::serializer   attributes :id, :artist_id, :label_id, :style_id, :title, :catalog,              :recording_date, :penguin, :category end 

from active_model_serializers documentation.

updated link. lowryder


Comments