在Rails 5中,通过比较现有列的值来添加新列。

huangapple go评论64阅读模式
英文:

Add new column by comparing values of an existing column in Rails 5

问题

我有一个模型,其中有一个名为 its_holiday 的列,是数据库中的一个 boolean 字段。现在我想添加另一个列 its_not_a_holiday,其值与 its_holiday 列的值相反。我应该如何将这个新列添加到现有的表中,并将其填充为与 its_holiday 列相反的值?

英文:

I have a model that has a column its_holiday that is a boolean field in database. Now I want to add another column its_not_a_holiday that has a value opposite to that of the its_holiday column. How can I add this new column to the existing table and populate it with the values opposite to that of its_holiday column?

答案1

得分: 3

如果你的老板坚持要有一个its_not_holiday布尔值(尽管其他答案和评论已经指出,你实际上不需要这样做),你可以按照以下方式进行迁移...

创建一个迁移

rails g migration AddItsNotAHolidayToMyModel its_not_a_holiday:boolean

编辑迁移如下

class AddItsNotAHolidayToMyModel < ActiveRecord::Migration[5.1] # 或者你所使用的版本
  def up
    add_column :my_model, :its_not_a_holiday, :boolean, default: false
    MyModel.where(its_holiday: [false, nil]).update_all(its_not_a_holiday: true)
  end
  def down
    remove_column :my_model, :its_not_a_holiday
  end
end

你可能需要修改模型以确保在保存记录时布尔值始终设置正确

class MyModel
  before_save :update_its_not_a_holiday

  private

  def update_its_not_a_holiday
    self.its_not_a_holiday = !its_holiday
  end
end
英文:

If your boss insists on having an its_not_holiday boolean (although other answers and comments have pointed out, you don't actually need to do that) you can do a migration as follows...

Create a migration

rails g migration AddItsNotAHolidayToMyModel its_not_a_holiday:boolean

Edit the migration as follows

class AddItsNotAHolidayToMyModel &lt; ActiveRecord::Migration[5.1] # or whatever version you have
  def up
    add_column :my_model, :its_not_a_holiday, :boolean, default: false
    MyModel.where(its_holiday: [false, nil]).update_all(its_not_a_holiday: true)
  end
  def down
    remove_column :my_model, :its_not_a_holiday
  end
end

You may want to modify the model to ensure the boolean is always set correctly when you save a record

class MyModel
  before_save :update_its_not_a_holiday

  private

  def update_its_not_a_holiday
    self.its_not_a_holiday = !its_holiday
  end
end

答案2

得分: 1

根据@Nithin提到的,您可以使用其_holiday的否定值而不是新列。为了遵循良好的实践,您应该使用否定值。但如果您仍然想这样做,您可以为现有记录这样做。

class AddItsNotAHolidayToModelName < ActiveRecord::Migration
  def up
    add_column :model_name, :its_not_a_holiday, :boolean
    ModelName.find_each do |model_name|
      model_name.its_not_a_holiday = !model_name.its_holiday
      model_name.save
    end
  end

  def down
    remove_column :model_name, :its_not_a_holiday
  end
end

对于新记录,

class ModelName
  before_save :update_its_not_a_holiday

  private

  def add_its_not_a_holiday
    self.its_not_a_holiday = !its_a_holiday
  end
end

希望对您有帮助。

英文:

As mentioned by @Nithin you can use negate value of its_holiday instead of new column. To follow good practice you should use the negate value. But if you still want to do that you can do it like this for existing records.

class AddItsNotAHolidayToModelName &lt; ActiveRecord::Migration
  def up
    add_column :model_name, :its_not_a_holiday, :boolean
    ModelName.find_each do |model_name|
      model_name.its_not_a_holiday = !model_name.its_holiday
      model_name.save
    end
  end

  def down
    remove_column :model_name, :its_not_a_holiday
  end
end

and for new records

class ModelName
  before_save :update_its_not_a_holiday

  private

  def add_its_not_a_holiday
    self.its_not_a_holiday = !its_a_holiday
  end
end

Hope it will work for you.

答案3

得分: 0

你可以在你的模型文件中添加一个自定义方法,像这样:

def its_not_a_holiday
  !its_holiday
end
英文:

You can add a custom method in your model file like this;

def its_not_a_holiday
  !its_holiday
end

huangapple
  • 本文由 发表于 2020年1月3日 18:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定