【问题标题】:Accessing method in mapping block for tire/elasticsearch轮胎/弹性搜索映射块中的访问方法
【发布时间】:2013-05-16 21:13:47
【问题描述】:

我有一个返回哈希的方法:

def image_hash
  images = {
    small: 'http://www.example.com/image1.png',
    medium: 'http://www.example.com/image2.png'
  }
end

我需要将它作为轮胎中 ActiveModel 映射的一部分进行索引,但我无法让它与 indexes 方法中的 as: 选项一起使用。

问题是我需要将其重新定义为图像(这是另一种模型方法),所以我正在尝试这样:

indexes :image_hash, as: :images do
  indexes :small, type: 'string', include_in_all: false
end

但是没有正确创建索引。

我知道to_indexed_json 方法的methods 选项:

def to_indexed_json
  to_json( methods: [:image_hash] )
end

哪个有效。但是如果不从头开始重新创建整个 JSON 对象,我看不到如何在该块中重新定义它。看起来偏好是完全将 mapping 块用于索引 JSON。

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails json elasticsearch tire


    【解决方案1】:

    我不太确定你想在那里做什么,但如果这是你想要做的,你不能动态地从哈希中取出一个项目

    你可能想使用类型对象,这样你就可以直接存储整个 has

    indexes :image_hash, type: object, include_in_all: false
    

    应该够了

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多