我该如何处理这种Active Record验证?

时间:2015-04-12 13:42:38

标签: ruby-on-rails ruby validation activerecord

我有三个模型,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

我想验证:user_id中的MessageReply是此sendor_id实例的receiver_id实例的Message还是MessageReply属于。

1 个答案:

答案 0 :(得分:1)

您使用custom validation方法并在MessageReply班级或您认为合适的地方实施逻辑。

validates :check_user

def check_user
  [custom logic]
end