【问题标题】:undefined method `serializable_hash' for String字符串的未定义方法“serializable_hash”
【发布时间】:2014-06-01 22:29:42
【问题描述】:

我正在尝试以下方式的 to_json

 @own_events.as_json(:include => {:created_date => {},:attendees => {}, :user => {}, :start_times => {}, :end_times =>{}} )

created_date 就是这样的方法

def created_date
  self.created_at.strftime('%d/%m/%Y %H:%M:%S')
end

因为我需要没有 TZD 的 created_at .. 但是有一个错误

NoMethodError(未定义方法 `serializable_hash' for "14/04/2014 16:04:07":String):

【问题讨论】:

    标签: ruby-on-rails serialization serializable


    【解决方案1】:

    :include 用于在 json 中包含关联,通常是 has_manybelongs_to 关联。

    要包含created_date 等方法,请使用methods:

    如:

    @own_events.as_json(include: [:attendees, :user, :start_times, :end_times], methods: :created_date )
    

    【讨论】:

    • SO 应该允许你再次投票,如果你上次看到答案已经一年多了,并且你发现它再次有用哈哈
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 2014-06-11
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多