Ruby on rails 2xu属于,B的唯一属性

Ruby on rails 2xu属于,B的唯一属性,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我想向ConversationParticipant添加验证,以便一个会话(conversation_id)只能存在同一用户(user_id)的一条记录。所以这是无效的: def User has_many :conversation_participants end def Conversation has_many :conversation_participants end def ConversationParticipant belongs_to :conversati

我想向ConversationParticipant添加验证,以便一个会话(conversation_id)只能存在同一用户(user_id)的一条记录。所以这是无效的:

def User
  has_many :conversation_participants
end

def Conversation
  has_many :conversation_participants
end

def ConversationParticipant
  belongs_to :conversation
  belongs_to :user
end

您可以将a:uniq传递到多个:

c = Conversation.first
c.conversation_participants.build(:user => User.first)
c.save # => true

c.conversation_participants.build(:user => User.first)
c.save # => false

您可以将一个:uniq传递到多个:

c = Conversation.first
c.conversation_participants.build(:user => User.first)
c.save # => true

c.conversation_participants.build(:user => User.first)
c.save # => false

验证:user\u id,:scope=>[:conversation\u id]的唯一性

或验证:

验证:user\u id,:university=>{:scope=>[:conversation\u id]}

验证:user\u id,:scope=>[:conversation\u id]的唯一性

或验证:

验证:user\u id,:university=>{:scope=>[:conversation\u id]}


“在上述情况下,仍然有两个读数。但是person.posts只显示一个帖子,因为集合只加载唯一的记录。”我不知道这是否会阻止一个不符合我的唯一性要求的记录,因为我没有尝试过(因为Andy的记录对我来说是完美的。)此外,我认为这种验证应该属于ConversationParticipant模型,但我认为这只是个人偏好。:)“在上述情况下,仍然有两个读数。但是person.posts只显示一个帖子,因为集合只加载唯一的记录。”我不知道它是否会阻止一个不符合我的唯一性要求的记录,因为我没有尝试过它(因为安迪的记录对我来说非常完美)此外,我认为这种验证应该属于ConversationParticipant模型,但我认为这只是个人偏好。:)
def User
  has_many :conversation_participants, :uniq => true
end

def Conversation
  has_many :conversation_participants, :uniq => true
end

def ConversationParticipant
  belongs_to :conversation
  belongs_to :user
end