【问题标题】:Is there a way to update an entire array or named_scope without using a loop in ruby on rails?有没有办法在不使用 ruby​​ on rails 循环的情况下更新整个数组或 named_scope?
【发布时间】:2010-11-06 05:23:21
【问题描述】:

我使用 searchlogic named_scopes 创建了以下数组:

todos = Todo.asset_is("Email").asset_id_is(self.id)

对于数组中的每个值,都有一个名为 original_date 和 current_date 的属性。

我需要对那些有一些逻辑的地方进行修改,比如:

difference = (original_date - date_entered) - self.days
original_date = date_entered + self.days
current_date = current_date - different

我不想做的是每个循环。但我不知道是否有替代方案——比如 SQL 中的“更新”(但不需要使用 SQL——比如使用 searchlogic)

【问题讨论】:

    标签: sql ruby-on-rails arrays


    【解决方案1】:
    Todo.update_all(["original_date = date_entered + %d, current_date = ... + %d", 
                     self.days, self.days], ["id in (?)", todos.map(&:id)])
    

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 2011-07-24
      • 2015-12-28
      • 1970-01-01
      • 2023-03-07
      • 2021-06-16
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      相关资源
      最近更新 更多