[英]If new active record has attributes, then create a has_many association
我有一個DoSomething
model。 這個 model 允許超級用戶告訴其他用戶在某個時間做某事。
我想允許超級用戶發送或者:
DoSomething
前 30 分鍾做DoSomething
DoSomething
2 小時后提醒其他用戶執行DoSomething
。 在DoSomething
model 上,我有用於提醒的布爾值。
創建DoSomething
后,我想檢查任一提醒是否為真,如果是,請從DoSomething
model 發送提醒。
我想知道檢查每個提醒然后創建提醒的機制。 提醒將是DoSomething
model 上的 has_many 關系。
如果您只關心在創建 DoSomething 時創建提醒,那么以下應該可以工作:
class DoSomething < ApplicationRecord
has_many :reminders
before_create :issue_reminders_if_needed
private
def issue_reminders_if_needed
return unless boolean_1 || boolean_2
# Code to create reminders goes here
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.