我有三个模型,user
,message
和message_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
我想验证:user_id
中的MessageReply
是此sendor_id
实例的receiver_id
实例的Message
还是MessageReply
属于。
答案 0 :(得分:1)
您使用custom validation方法并在MessageReply
班级或您认为合适的地方实施逻辑。
validates :check_user
def check_user
[custom logic]
end