代码之家  ›  专栏  ›  技术社区  ›  mhatch

如何在Rails迁移中更新现有引用上的选项

  •  0
  • mhatch  · 技术社区  · 4 年前

    例如,我有:

    class Chapter < ApplicationRecord
      belongs_to :org
    end
    

    我想更新到:

    class Chapter < ApplicationRecord
      belongs_to :org, touch: true
    end
    

    如何为此编写迁移文件?(或任何其他参考选项更改?)

    class AddChapterToOrg < ActiveRecord::Migration
      def change
        add_reference :org, :chapter, touch: true
      end
    end
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   spickermann    4 年前

    touch: true belongs_to 方法,并告诉Rails在更新当前对象的时间戳时更新关联对象的时间戳。这种触摸由Rails处理,而不是由数据库引擎处理。

    也就是说,当你添加 触摸:真实 到a 然后,无需运行数据库迁移,因为不需要更改数据库模式来支持这一点。