I'm adding a multiplier column to one of my models, and it should have a default value of 1. I'm wondering if it's preferable to add the default value at the database layer or in a callback in my model. I'm leaning toward model in the name of database agnosticity (if that's a word, which I guess it's not). Am I right to do so?
Since you can declare the default in a Rails migration and migrations use database adapters, setting the default in a migration is sufficiently database agnostic for most purposes. Eg:
class AddMultiplierColumn < ActiveRecord::Migration
def change
add_column :my_table, :multiplier, :float, default: 1.0
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.