2015-04-12 71 views
1

我有三個型號,usermessagemessage_reply我該如何處理這個活動記錄驗證?

#user.rb 

class User < ActiveRecord::Base 
    has_many :message_replies 
    has_many :sent_messages, class_name: "Message", foreign_key: :sendor_id 
    has_many :received_messages, class_name: "Message", foreign_key: :receiver_id 
end 


#message.rb 

class Message < ActiveRecord::Base 
    belongs_to :Sendor, class_name: "user", foreign_key: :sendor_id 
    belongs_to :Receiver, class_name: "user", foreign_key: :receiver_id 
    has_many :message_replies 
end 

#message_reply.rb 

class MessageReply < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :message 
end 

我想驗證在MessageReply:user_id要麼是sendor_idMessage實例receiver_id,這MessageReply實例所屬。

+0

「MessageReply」的用途是什麼?它是什麼時候創建的?創建時,它是否已經與'message'關聯? – AbM

+0

@AbM'MessageReply'是回覆特定'Message'的模型。它與我在問題中發佈的模型代碼所示的消息具有一對多關係。 –

回答

1

您使用custom validation方法並在您的MessageReply類或任何您認爲合適的位置實施邏輯。

validates :check_user 

def check_user 
    [custom logic] 
end