ruby on rails - pass in options hash via ArraySerializer new syntax -
is possible call arrayserializer constructor this:
mi_tmp[:notes]=activemodel::arrayserializer.new(mi.notes, each_serializer: notesserializer, show_extra:false)
and in serializer:
..... if @options[show_extra] attributes :user_id end
i error:
error: undefined local variable or method `show_extra' notesserializer:class
but can't find example using type of syntax. thx
edit 1
first thing tried no dice:
mi_tmp[:notes]=activemodel::arrayserializer.new(mi.notes, each_serializer: notesserializer, @options{ show_extra: false } )
this might helpfull , work fine me
1.in books controller
# books contain collection of books books = serialize books, bookserializer, {root: false, user_book_details: user_book_details} # custom method def serialize data, serializer, options = {} data.map |d| serializer.new(d, options) end end
2.in book serializer
class bookserializer < activemodel::serializer def initialize object, options = {} @user_book_details = options[:user_book_details] super object, options end attributes :id, :title, :short_description, :author_name, :image, :time_ago, :has_audio, :book_state # can access @user_book_details anywhere inside serializer end
Comments
Post a Comment