【问题标题】:Rails 4: Order has_many in serializerRails 4:在序列化程序中订购 has_many
【发布时间】:2017-11-30 21:09:40
【问题描述】:

我有一个带有has_many 关联的序列化程序,我想订购依赖模型。出于某种原因,我收到 undefined method key?' 错误:

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

我如何按周订购签到?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 serialization has-many


    【解决方案1】:
    class API::ClientSerializer < ActiveModel::Serializer
      has_many :check_ins
    
      def check_ins
        object.check_ins.order(:week)
      end
    end
    

    【讨论】:

    • 有趣的是你推荐了这个,这就是我最终做的我只是认为有一种方法可以修复初始语法
    • 有没有办法排序和使用 check_ins 序列化程序?
    【解决方案2】:
    class API::ClientSerializer < ActiveModel::Serializer
      has_many :check_ins do
        object.check_ins.order(:week)
      end 
    end
    

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      相关资源
      最近更新 更多