【问题标题】:How to build custom to_indexed_json method for Tire?如何为轮胎构建自定义 to_indexed_json 方法?
【发布时间】:2013-01-12 00:02:48
【问题描述】:

我想实现这样的目标:

  def to_indexed_json
    { 
      if source.respond_to?(:app_id)
        app_id: source.app_id
      end
      id: self.id,
      content: self.content
    }.to_json
  end

【问题讨论】:

    标签: ruby-on-rails ruby json elasticsearch tire


    【解决方案1】:
    def to_indexed_json
      result = {id: self.id, content: self.content}
      result.merge!(app_id: source.app_id) if source.respond_to?(:app_id)
      result.to_json
    end
    

    【讨论】:

      【解决方案2】:
      data = Hash.new
      if source.respond_to?(:app_id)
        data['app_id']: source.app_id
      end
      data['id']: self.id,
      data['content']: self.content
      data.to_json
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        相关资源
        最近更新 更多