2017-06-27 17 views
0

我在Rails中的幾個模型,像這樣關聯的相關記錄:生成時與另一個記錄被標記爲「完成」的Rails

class Scan < ApplicationRecord 
    belongs_to :project 
    has_many :ats 
end 

class At < ApplicationRecord 
    belongs_to :project 
    belongs_to :scans 
end 

我的問題是: 我怎麼有Rails的產生AT時掃描被用戶標記爲「完成」?應生成與已標記爲「完成」的掃描關聯的AT。

更新時間: 掃描被標記爲「完整的」使用布爾

提前感謝!

+0

你怎麼標誌着'Scan'爲完成? – gabrielhilal

+0

@gabrielhilal我已更新我的問題 – kmaune

回答

1

,你可以這樣做以下:

class Scan < ApplicationRecord 
    belongs_to :project 
    has_many :ats 
    after_update :generate_at 

    private 

    def generate_at 
    return unless self.complete 
    self.ats.create! 
    end 
end 
+0

謝謝你指點我正確的方向!我會給這個鏡頭 – kmaune