【发布时间】:2023-04-10 17:06:01
【问题描述】:
我有一个公司表,我想检查是否存在多个列以删除它们。
class RemoveSettingsReportColumnsFromCompany < ActiveRecord::Migration[5.1]
def change
if column_exists?( :companies, :automatically_send_report ) && column_exists?( :companies, :time_limit_for_sending_report ) && column_exists?( :companies, :report_fragment_color ) && column_exists?( :companies, :display_default_avatar )
remove_column :companies, :automatically_send_report, :boolean
remove_column :companies, :time_limit_for_sending_report, :integer
remove_column :companies, :report_fragment_color, :string
remove_column :companies, :display_default_avatar, :boolean
end
end
end
正如您所见,函数 column_exists 的使用存在冗余,我想更好地组织和减少我的代码,有人可以告诉我是否有使用 column_exists 的方法 以其他方式,并将其作为我要删除的所有列的参数?
【问题讨论】:
标签: ruby-on-rails ruby database migration