【问题标题】:BSON::InvalidObjectId illegal ObjectId formatBSON::InvalidObjectId 非法 ObjectId 格式
【发布时间】:2023-03-27 07:17:01
【问题描述】:

尝试在我的控制器中插入一个简单的if 条件后出现此错误:

def index
  if params[:query].present?
    @blog_posts = BlogPost.search(params[:query])
  else
    @blog_posts = BlogPost.all
  end 
end

【问题讨论】:

  • 我正在使用轮胎(弹性搜索)作为搜索引擎。 params[:query] 是我为搜索键而写的

标签: ruby-on-rails elasticsearch tire


【解决方案1】:

取决于您的模型是如何设置的。正如Tire README 所说,Mongoid 的 ID 有点古怪,您可能需要自己定义 to_indexed_json

def to_indexed_json
  self.as_json
end

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多