简体   繁体   English

Rails协会-多个用户在单个模型中扮演不同的角色

[英]Rails Associations - Multiple users fulfilling different roles for single model

I'm struggling with associations today. 我今天在与协会做斗争。

I have 2 models 我有2个型号

User
Match

A match is initiated by a userA and has an opponent, userB 比赛是由用户A发起的,有对手userB

I need to be able to distinguish which user created the match and which is the opposer so simple has_many & belongs_to isn't enough. 我需要能够区分是哪个用户创建了匹配项,哪个是反对者,所以简单的has_manybelongs_to是不够的。

Ok i would do something like this: 好吧,我会做这样的事情:

on my create Match action i would create the match like this 在我的“创建匹配”操作上,我将像这样创建匹配

Match controller: 比赛控制器:

def create
   @match= Match.new(params[:match])
   @match.creator_id = current_user.id 

    respond_to do |format|
      if @match.save
        format.html    
      else
        format.html
      end
    end
end

asuming you are using something like devise and that the match model has both of the users and one is the creator_id 假设您使用的是诸如devise之类的内容,并且匹配模型同时具有两个用户,其中一个是creator_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM