Rails 数据库 table 更改了名称,但另一个 table 中的索引指的是旧名称

Rails database table changed name, but index in another table refers to old name

我将 table 名称从 "Addresses" 更改为 "Locations"。这是两个模型

class Location < ActiveRecord::Base
  has_many :years, dependent: :destroy 
  has_many :people, through: :years 

class Year < ActiveRecord::Base
  belongs_to :location
  belongs_to :person

我通过 rename_table :addresses, :locations 迁移进行了更改,并更改了相关控制器和视图中的文件夹、文件和所有引用的名称。除了 Years 数据库在年份 table 中仍然有一个列 address_id 之外,大多数一切似乎都正常。显然需要location_id。我可以只迁移更改吗?是否会保留指向新命名数据库的链接?我已经备份了数据库并制作了 Years 输出的电子表格副本。

项目的预更改版本位于 https://bitbucket.org/MtnBiker/crores5/src。在整理好之前,我不愿意将更改合并到 master 中。

感谢您的建议。

PS。我不建议更改 table 名称,但我在地址 table 中有一个名为 address 的字段,我想摆脱这种混乱。不用说了,我是新手

我认为您正在寻找的是简单地重命名 Years table:

中的列
rename_column :table_name, :old_column, :new_column

也许

rename_column :years, :address_id, :location_id

迁移后 table 年将有一个 location_id 列,该列的值与之前的值相同。

编辑:不需要任何列具有任何特定名称(尽管如果您的应用使用 "rails way). You can, for example, define the Years model to reference the Location model via a column that has an " 不正确的名称几乎总是更容易。

class Year < ActiveRecord::Base
  belongs_to :location, foreign_key: :address_id, primary_key: :id