0
我有用戶模型(has_one:個人資料)和個人資料模型(belongs_to:用戶)。欄改變列條件的缺省值
我想從:none if user.user_type != "customer"
將默認值profile.deliver_project_news
更改爲:twice_a_week
。
我可以通過遷移來處理這個問題嗎?如果是,那我該怎麼做?
我有用戶模型(has_one:個人資料)和個人資料模型(belongs_to:用戶)。欄改變列條件的缺省值
我想從:none if user.user_type != "customer"
將默認值profile.deliver_project_news
更改爲:twice_a_week
。
我可以通過遷移來處理這個問題嗎?如果是,那我該怎麼做?
在我看來,你不應該把這個邏輯放到你的數據庫中。我認爲它應該進入你的模型層
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