处理遗留数据库中的列名 'hash'
Deal with a column name 'hash' in a legacy database
我必须从 Rails 5.2 项目中访问遗留数据库。不幸的是,我无法更改任何 table 列名称,并且 table 包含一个名称为 hash
的列,它不适用于 ActiveRecord(将抛出错误,因为 hash
这是一个现有的方法)。我不需要该列,但我既不能重命名也不能删除它。
有没有办法告诉 ActiveRecord 不要使用给定 table 的 hash
字段?
您可以使用在 5.0 版 Rails 上添加到 Ruby 的 ignored_columns
方法来忽略数据库中的列。引自文档:
ignored_columns=(columns)
Sets the columns names the model should ignore. Ignored columns won't have attribute accessors defined, and won't be referenced in SQL queries.
只需将以下内容添加到您的模型中:
class MyModel < ApplicationRecord
self.ignored_columns = %w(hash)
end
我必须从 Rails 5.2 项目中访问遗留数据库。不幸的是,我无法更改任何 table 列名称,并且 table 包含一个名称为 hash
的列,它不适用于 ActiveRecord(将抛出错误,因为 hash
这是一个现有的方法)。我不需要该列,但我既不能重命名也不能删除它。
有没有办法告诉 ActiveRecord 不要使用给定 table 的 hash
字段?
您可以使用在 5.0 版 Rails 上添加到 Ruby 的 ignored_columns
方法来忽略数据库中的列。引自文档:
ignored_columns=(columns)
Sets the columns names the model should ignore. Ignored columns won't have attribute accessors defined, and won't be referenced in SQL queries.
只需将以下内容添加到您的模型中:
class MyModel < ApplicationRecord
self.ignored_columns = %w(hash)
end