我必须从 Rails 5.2 项目中访问遗留数据库。不幸的是,我无法更改任何表列名称,并且该表包含一个名称为 hash
的列,它不适用于 ActiveRecord(因为 hash
而会抛出错误是一种现有的方法)。我不需要该列,但我既不能重命名也不能删除它。
有没有办法告诉 ActiveRecord 不要使用给定表的 hash
字段?
最佳答案
您可以使用 ignored_columns
在 5.0 版中添加到 Ruby on Rails 中的方法,用于忽略数据库中的列。引用自文档:
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
关于ruby-on-rails - 处理旧数据库中的列名 'hash',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889299/