2011-12-19 58 views
0

Rails 3中,我需要保存當前對象並創建另一個

recommendations has_many approvals 

基本上是一個被推薦使用的批准創建的。可以批准的人進入並檢查批准框,並且需要爲需要批准的下一個人輸入電子郵件地址(電子郵件是批准的屬性)。

需要注意的是,如果current_user具有user_type = SMT,則不需要更多批准。這是最後的批准。

我使用的是建議/:id/approval /:id/edit動作。我認爲我只需要一個批准方法。喜歡的東西:

before_save :save_and_create 

def save_and_create 
Some code that saves the current approval, creates a new one and asks me for the next admins email address, and send that user an email requesting that they approve 
end 

任何幫助,將不勝感激

回答

0

我終於想通這一個。我只是創造了一個before_save回調下面的方法:

def create_next_approval 
     next_approval = self.recommendation.approvals.build(:email => self.next_approver_email, :user_id => User.find_by_email(next_approver_email)) 
     next_approval.save if next_approver_email.present? && recently_approved? 
    end 

希望它可以幫助其他人在我的鞋。

0
# old post 
class Recommendation << AR 
    validate :approval_completed? 

    def approval_completed? 
    if self.approvals.last.user.type == "SMT" 
     return true 
    else 
     return false # or a number for needed approvals: self.approvals.count >= 5 
    end 
    end 
end 

# new solution 
class Approval << AR 
    validate :approvals_completed? 

    def approvals_completed? 
    if self.recommendation.approvals.last.user.type == "SMT" 
     return true 
    else 
     return false # or a number for needed approvals: self.approvals.count >= 5 
    end 
    end 
end 
+0

哦,我看到你想要一個類的批准方法..我會改變這一點。 – Lichtamberg 2011-12-19 23:11:41

+0

這看起來像我正在嘗試做的第二部分...第一部分是保存並要求用戶發送新電子郵件...我可以處理髮送電子郵件,但不知道如何保存並創建另一個 – 2011-12-19 23:24:35

+0

你的意思是,批准,當添加一個新的電子郵件地址時,批准應該向下一個批准者發送一個emial? – Lichtamberg 2011-12-20 00:17:57

相關問題