【问题标题】:Mongoid Search Gem does not update keywords on associationMongoid Search Gem 不会更新关联关键字
【发布时间】:2021-09-23 20:18:30
【问题描述】:

我有一个电子商务应用程序,我在其中使用 mongoid 搜索来搜索订单及其用户关联。这是我的模型。

order.rb

class Order
   include Mongoid::Document
   include Mongoid::Search

   belongs_to :user

   search_in user: %i[first_name last_name email_address]
end

user.rb

class User
   include Mongoid::Document
   include Mongoid::Search

   has_many :orders

   search_in :email_address, :first_name, :last_name
end

基本上,使用搜索的最终目标是通过用户的名字、姓氏和电子邮件地址来查找用户的订单。在初始阶段,我可以创建一个用户和订单,并填充 mongoid 用于搜索的 keywords

但是,如果我更新用户的名字、姓氏或电子邮件地址,Order 的关键字不会更新。因此搜索中断,准确率下降。

根据文档,我可以执行以下命令rake mongoid_search:index,关键字将再次填充正确的关键字。

但是,在生产中,我无法在每次发生变化时都运行此命令。我需要它自动更新。如果我更新关联的对象关键字不会改变,我是否做错了什么?

【问题讨论】:

    标签: ruby-on-rails mongoid


    【解决方案1】:

    查看source,您应该能够保存受影响的模型(例如,更改用户后,查找并保存他们的所有订单)并且关键字将自动更新。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多