Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: Order has_many in serializer

I have a serializer with a has_many association and I want to order the dependent model. For some reason I get an undefined method key?' error:

class API::ClientSerializer < ActiveModel::Serializer
    has_many :check_ins, -> { order(:week) }
end

How else do I order the check_ins by week?

like image 713
Jeremy Thomas Avatar asked Jun 27 '17 17:06

Jeremy Thomas


1 Answers

class API::ClientSerializer < ActiveModel::Serializer
  has_many :check_ins do
    object.check_ins.order(:week)
  end 
end
like image 81
fabro Avatar answered Nov 03 '22 01:11

fabro