Ruby on rails rails随条件更改列默认值

Ruby on rails rails随条件更改列默认值,ruby-on-rails,migration,Ruby On Rails,Migration,我有用户模型(has_one:profile)和配置文件模型(归属于:User) 我想更改配置文件的默认值。如果user.user\u type!=“客户” 我可以用迁移来处理这个问题吗?如果是的话,我该怎么做呢?在我看来,您不应该把这个逻辑放在数据库中。我想它应该放在你的模型层里 class Profile < ActiveRecord::Base before_validate: set_defaults def set_defaults if us

我有用户模型(has_one:profile)和配置文件模型(归属于:User)

我想更改
配置文件的默认值。如果user.user\u type!=“客户”


我可以用迁移来处理这个问题吗?如果是的话,我该怎么做呢?

在我看来,您不应该把这个逻辑放在数据库中。我想它应该放在你的模型层里

class Profile < ActiveRecord::Base

    before_validate: set_defaults

    def set_defaults
        if user.user_type != "customer"
            deliver_project_news ||= "twice_a_week"
        else
            deliver_project_news ||= "none"
        end
    end
end
类配置文件